Python Basics
Use the Template
to play around with the different basic building blocks of Python.
Create new cells with # %%
as necessary.
Use the Python Basics section and the reference for help.
# %%# Types:# Boolean (bool), Integer (int), Range (range), Float (float),# Complex Number (complex), String (str), List (list), Tuple (tuple),# Dictionary (dict), Set (set), Absence of value (None)
# %%# Variables and Assignment (=)
# %%# Mathematical Operations (+,-,*,**,/,//)
# %%# Comparisons (==,!=,<,<=,>,>=)
# %%# Boolean Operators (and/or/not)
# %%# String Expressions (+,*)
# %%# List Expressions (+,*)
# %%# Conditional Execution (if/elif/else)
# %%# For Loops (for)
# %%# While Loops (while)
# %%# List Comprehensions [...]
# %%# Dictionary Comprehensions {...}
# %%# Functions (def)
# %%# Anonymous Functions (lambda)
# %%# Import Packages (import)
# %%# List Indexing
# %%# Dictionary Indexing
# %%# Types:# Boolean (bool), Integer (int), Range (range), Float (float),# Complex Number (complex), String (str), List (list), Tuple (tuple),# Dictionary (dict), Set (set), Absence of value (None)
# Boolean (bool)True, False
# Integer (int)15
# Range (range)range(1, 10, 2)
# Float (float)2.66666
# Complex Number (complex)1.5 + 3.2j
# String (str)"Text", "Text"
# List (list)[1, 2, 3]
# Tuple (tuple)(1, 2, 3)
# Dictionary (dict){"a": 1, "b": 2, "c": 3}
# Set (set){"a", "b", "c"}
# Absence of value (None)None
# %%# Variables and Assignment (=)a = 1b = 4
c, d = 13, 14
# %%# Mathematical Operations (+,-,*,**,/,//)1 + 1a - 2
b / cb // 3 # Floor division
c * dc**2 # Power
# %%# Comparisons (==,!=,<,<=,>,>=)a == 1 # Truea != 1 # False
a < 2 # Truea <= 2 # True
a > 2 # Falsea >= 2 # False
# %%# Boolean Operators (and/or/not)a == 1 and not (b < 3 or b > 3)
# %%# String Expressions (+,*)"My name is " + "Peter"
"A" * 5 # "AAAAA"
# %%# List Expressions (+,*)["one", "list"] + ["two", "lists"]
["A"] * 5
# %%# Conditional Execution (if/elif/else)if a == b: print("a = b")elif a > b: print("a > b")else: print("a < b")
# %%# For Loops (for)for i in range(10): print(i)
# %%# While Loops (while)j = 0while j < 10: print(j) j += 1 # j = j + 1
# %%# List Comprehensions [...]lc = [i for i in range(5)]
# Same asll = []for i in range(5): ll.append(i)
lc == ll
# %%# Dictionary Comprehensions {...}keys = ["A", "B", "C"]values = [1, 2, 3]
dc = {k: j for k, j in zip(keys, values)}
# Same as
dl = {}for k, j in zip(keys, values): dl[k] = j
dc == dl
# %%# Functions (def)def my_function(a, b=2): return a**b
my_function(2)my_function(2, 3)
# %%# Anonymous Functions (lambda)my_afunc = lambda x: x + 1
my_afunc(1)
# %%# Import Packages (import)import pandas as pd
pd.DataFrame()
# %%# List Indexingmy_list = [1, 2, 3, 4, 5, 6, 7]
len(my_list)
my_list[0]my_list[3]
my_list[-1]my_list[-2]
my_list[1:3]my_list[1:6:2]
# %%# Dictionary Indexingmy_dict = {"A": 1, "B": 2, "C": 3}
len(my_dict)
my_dict["A"]my_dict["C"]