Setting Up Jupyter Notebook
Launch Jupyter through a terminal session. Before starting, create and activate a dedicated Conda environment:
conda create -n myenv python=3.10
conda activate myenv
jupyter notebook
Python Syntax Essentials
Multiple statements can be placed on one line by separating them with a semicolon:
print('hello'); print('world')
Indentation defines code blocks. The standard is to use a consistent number of spaces at the beginning of a line.
Line continuation is achieved with a backslash \ outside of brackets:
calculation = value_a + \
value_b + \
value_c
Inside [], {}, or (), no backslash is needed for multi-line constructions:
week_days = ['Monday', 'Tuesday', 'Wednesday',
'Thursday', 'Friday']
String Literals
single_word = 'word'
whole_sentence = "This is a sentence."
multiple_lines = """This is a paragraph.
It spans multiple lines."""
Comments
Use # for a single line, and triple quotes ''' or """ for multi-line doucmentation strings.
Printing Without a Newline
print first_item,
print second_item,
In Python 3, the end argument suppresses the newline:
print('item', end=' ')
print('next', end=' ')
Conditionals
if condition_a:
suite
elif condition_b:
suite
else:
suite
Core Data Structures
Strings
Slicing follows [start:end:step]. Positive indices move left to right; negative indices move right to left.
text = "adada"
segment = text[1:4] # 'dad'
reverse_segment = text[-1:-4:-1] # 'ada'
doubled = text * 2 # 'adadaadada'
Lists
Created with square brackets. Lists are mutable and support slicing.
example_list = [1, 'two', 3.0]
Tuples
Created with parentheses. Tuples are immutable; elements cannot be reassigned.
example_tuple = (10, 20, 30)
Dictionaries
Created with curly braces and hold key-value pairs.
# -*- coding: UTF-8 -*-
my_dict = {}
my_dict['one'] = "This is one"
my_dict[2] = "This is two"
tiny = {'name': 'runoob', 'code': 6734, 'dept': 'sales'}
print(my_dict['one'])
print(my_dict[2])
print(tiny)
print(tiny.keys())
print(tiny.values())
Output:
This is one
This is two
{'dept': 'sales', 'code': 6734, 'name': 'runoob'}
['dept', 'code', 'name']
['sales', 6734, 'runoob']
Operators
**performs exponentiation.3 ** 2yields9.//performs floor division.9 // 2yields4.
Identity Operators
is checks if two names point to the exact same object (same memory location).
if a is b:
print("They share the same memory space")
is not verifies that two names reference different objects.