Try / Python in Y minutes
Python was created by Guido van Rossum in the early 90s. It is now one of the most popular languages in existence. I fell in love with Python for its syntactic clarity. It's basically executable pseudocode.
✨ This is an open source guide. Feel free to improve it!
Datatypes and Operators · Variables and Collections · Control Flow and Iterables · Functions · Modules · Classes · Generators and Decorators · Further Reading
1. Datatypes and Operators
There are two ways to make comments:
# Single line comments start with a number symbol.
"""
Multiline strings can be written
using three "s, and are often used
as documentation.
"""
You have numbers:
print(3)
3
Math operates as you'd expect:
a = 1 + 1
b = 2 - 1
c = 4 / 2
d = 3 * 2
print(a, b, c, d)
2 1 2.0 6
Floor division rounds towards negative infinity:
a = 5 // 3
b = -5 // 3
c = 5.0 // 3.0
d = -5.0 // 3.0
print(a, b, c, d)
1 -2 1.0 -2.0
The result of division is always a float:
x = 10 / 3
print(x)
3.3333333333333335
Unless you use an integer division:
x = 10 // 3
print(x)
3
Modulo operation:
x = 7 % 3
print(x)
1
Exponentiation (x**y, x to the yth power):
x = 2**3
print(x)
8
Enforce precedence with parentheses:
x = 1 + 3 * 2
y = (1 + 3) * 2
print(x, y)
7 8
Boolean values are primitives (Note: the capitalization):
x = True
y = False
print(x, y)
True False
Negate with not:
x = not True
y = not False
print(x, y)
False True
Note "and" and "or" are case-sensitive:
x = True and False
y = False or True
print(x, y)
False True
True and False are actually 1 and 0 but with different keywords:
x = True + True
y = True * 8
z = False - 5
print(x, y, z)
2 8 -5
Comparison operators look at the numerical value of True and False:
x = 0 == False
y = 2 > False
z = 2 == 2
print(x, y, z)
True True True
None, 0, and empty strings/lists/dicts/tuples/sets all evaluate to False. All other values are True:
print(bool(0))
print(bool(""))
print(bool([]))
print(bool({}))
print(bool(()))
print(bool(set()))
print(bool(4))
print(bool(-6))
False
False
False
False
False
False
True
True
Using boolean logical operators on ints casts them to booleans for evaluation, but their non-cast value is returned. Don't mix up with bool(ints) and bitwise and/or (&,|):
print(bool(0))
print(bool(2))
print(0 and 2)
print(bool(-5))
print(bool(2))
print(-5 or 0)
False
True
0
True
True
-5
Equality is ==:
print(1 == 1)
print(2 == 1)
print(1 == 1)
print(2 == 1)
Inequality is !=:
print(1 != 1)
print(2 != 1)
False
True
More comparisons:
print(1 < 10)
print(1 > 10)
print(2 <= 2)
print(2 >= 2)
True
False
True
True
Seeing whether a value is in a range:
print(1 < 2 and 2 < 3)
True
Chaining makes this look nicer:
print(1 < 2 < 3)
True
(is vs. ==) is checks if two variables refer to the same object, but == checks if the objects pointed to have the same values:
a = [1, 2, 3, 4] # Point a at a new list, [1, 2, 3, 4]
b = a # Point b at what a is pointing to
print(b is a) # => True, a and b refer to the same object
print(b == a) # => True, a's and b's objects are equal
b = [1, 2, 3, 4] # Point b at a new list, [1, 2, 3, 4]
print(b is a) # => False, a and b do not refer to the same object
print(b == a) # => True, a's and b's objects are equal
True
True
False
True
Strings are created with " or ':
print("This is a string.")
print('This is also a string.')
This is a string.
This is also a string.
Strings can be added too:
print("Hello " + "world!")
# String literals (but not variables)
# can be concatenated without using '+'.
print("Hello " "world!")
Hello world!
Hello world!
A string can be treated like a list of characters:
print("Hello world!"[0])
H
You can find the length of a string:
print(len("This is a string"))
16
You can use f-strings or formatted string literals:
name = "Reiko"
print(f"She said her name is {name}.")
# Any valid Python expression inside
# these braces is returned to the string.
print(f"{name} is {len(name)} characters long.")
She said her name is Reiko.
Reiko is 5 characters long.
None is an object:
print(None, type(None))
None <class 'NoneType'>
Don't use the equality "==" symbol to compare objects to None. Use "is" instead. This checks for equality of object identity:
print("etc" is None)
print(None is None)
False
True
2. Variables and Collections
As you've seen from the last section, Python has a print function:
print("I'm Python. Nice to meet you!")
I'm Python. Nice to meet you!
By default the print function also prints out a newline at the end. Use the optional argument end to change the end string:
print("Hello, World", end="!")
Hello, World!
Simple way to get input data from console:
input_string_var = input("Enter some data: ")
print(input_string_var)
There are no declarations, only assignments. Convention in naming variables is snake_case style:
some_var = 5
print(some_var)
5
Accessing a previously unassigned variable is an exception:
print(some_unknown_var)
Traceback (most recent call last):
File "/sandbox/main.py", line 1, in <module>
print(some_unknown_var)
^^^^^^^^^^^^^^^^
NameError: name 'some_unknown_var' is not defined (exit status 1)
See Control Flow to learn more about exception handling.
if can be used as an expression, equivalent of C's '?:' ternary operator:
print("yay!" if 0 > 1 else "nay!")
nay!
Lists store sequences. You can start with a prefilled list:
li = []
other_li = [4, 5, 6]
print(li)
print(other_li)
[]
[4, 5, 6]
Add stuff to the end of a list with append:
li = []
li.append(1)
li.append(2)
li.append(4)
li.append(3)
print(li)
[1, 2, 4, 3]
Remove from the end with pop:
li = [4, 7, 32]
print(li.pop())
print(li)
32
[4, 7]
Access a list like you would any array:
li = [4, 7, 32]
print(li[0])
print(li[-1]) # [-1] gives the last element
4
32
Looking out of bounds is an IndexError:
li = [4, 7, 32, 8]
print(li[4])
Traceback (most recent call last):
File "/sandbox/main.py", line 2, in <module>
print(li[4])
~~^^^
IndexError: list index out of range (exit status 1)
You can look at ranges with slice syntax. The start index is included, the end index is not (It's a closed/open range for you mathy types):
li = [100, 200, 300, 400]
print(li[1:3]) # list from index 1 to 3 => [2, 4]
print(li[2:]) # list starting from index 2 => [4, 3]
print(li[:3]) # list from beginning until index 3 => [1, 2, 4]
print(li[::2]) # list selecting elements with a step size of 2 => [1, 4]
print(li[::-1]) # list in reverse order => [3, 4, 2, 1]
[200, 300]
[300, 400]
[100, 200, 300]
[100, 300]
[400, 300, 200, 100]
Make a one layer deep copy using slices:
li = [4, 7, 32, 8]
li2 = li[:]
print(li2)
print(li2 is li) # False
[4, 7, 32, 8]
False
Remove arbitrary elements from a list with "del":
li = [4, 7, 32, 8]
del li[2]
print(li)
[4, 7, 8]
Remove first occurrence of a value:
li = [4, 7, 32, 8]
li.remove(32)
print(li)
# Uncommenting the next line will raise a ValueError
# li.remove(2)
[4, 7, 8]
Insert an element at a specific index:
li = [4, 7, 32, 8]
li.insert(1, 2)
print(li)
[4, 2, 7, 32, 8]
Get the index of the first item found matching the argument:
li = [4, 7, 32, 8]
print(li.index(7))
# Uncommenting the next line will raise a ValueError
# print(li.index(36))
1
You can add lists (Note: values for li and for other_li are not modified):
li = [4, 7, 32, 8]
other_li = [1, 2, 3, 4]
print(li + other_li)
[4, 7, 32, 8, 1, 2, 3, 4]
Concatenate lists with "extend()":
li = [4, 7, 32, 8]
other_li = [1, 2, 3, 4]
li.extend(other_li)
print(li)
[4, 7, 32, 8, 1, 2, 3, 4]
Check for existence in a list with "in":
li = [4, 7, 32, 8]
print(1 in li)
False
Examine the length with "len()":
li = [4, 7, 32, 8]
print(len(li))
4
Tuples are like lists but are immutable:
tup = (1, 2, 3)
print(tup[1])
# Uncommenting the next line will raise a TypeError
# tup[1] = 7
2
Note that a tuple of length one has to have a comma after the last element but tuples of other lengths, even zero, do not:
print(type((1)))
print(type((1,)))
print(type(()))
<class 'int'>
<class 'tuple'>
<class 'tuple'>
You can do most of the list operations on tuples too:
tup = (1, 2, 3)
print(len(tup))
print(tup + (4, 5, 6))
print(tup[:2])
print(2 in tup)
3
(1, 2, 3, 4, 5, 6)
(1, 2)
True
You can unpack tuples (or lists) into variables:
a, b, c = (1, 2, 3)
print(a, b, c)
# Extended unpacking.
a, *b, c = (1, 2, 3, 4)
print(a, b, c)
# Tuples are created by default if you leave out the parentheses.
d, e, f = 4, 5, 6
print(d, e, f)
# Tuple 4, 5, 6 is unpacked into variables d, e and f
# respectively such that d = 4, e = 5 and f = 6
# Now look how easy it is to swap two values:
e, d = d, e
print(d, e)
1 2 3
1 [2, 3] 4
4 5 6
5 4
Dictionaries store mappings from keys to values:
empty_dict = {}
filled_dict = {"one": 1, "two": 2, "three": 3}
print(empty_dict)
print(filled_dict)
{}
{'one': 1, 'two': 2, 'three': 3}
Note keys for dictionaries have to be immutable types. This is to ensure that the key can be converted to a constant hash value for quick look-ups. Immutable types include ints, floats, strings, tuples:
valid_dict = {(1, 2, 3): "123"}
print(valid_dict)
# Uncommenting the next line will raise a TypeError
# invalid_dict = {[1, 2, 3]: "123"}
{(1, 2, 3): "123"}
Look up values with []:
filled_dict = {"one": 1, "two": 2, "three": 3}
print(filled_dict["one"])
1
Get all keys as an iterable with "keys()". We need to wrap the call in list() to turn it into a list. Dictionary items maintain the order at which they are inserted into the dictionary:
filled_dict = {"one": 1, "two": 2, "three": 3}
print(list(filled_dict.keys()))
['one', 'two', 'three']
Get all values as an iterable with "values()". Once again we need to wrap it in list() to get it out of the iterable:
filled_dict = {"one": 1, "two": 2, "three": 3}
print(list(filled_dict.values()))
[1, 2, 3]
Check for existence of keys in a dictionary with "in":
filled_dict = {"one": 1, "two": 2, "three": 3}
print("one" in filled_dict)
print(1 in filled_dict)
True
False
Looking up a non-existing key is a KeyError:
filled_dict = {"one": 1, "two": 2, "three": 3}
print(filled_dict["four"])
Traceback (most recent call last):
File "/sandbox/main.py", line 2, in <module>
print(filled_dict["four"])
~~~~~~~~~~~^^^^^^^^
KeyError: 'four' (exit status 1)
Use "get()" method to avoid the KeyError:
filled_dict = {"one": 1, "two": 2, "three": 3}
print(filled_dict.get("one"))
print(filled_dict.get("four"))
print(filled_dict.get("one", 4))
print(filled_dict.get("four", 4))
1
None
1
4
"setdefault()" inserts into a dictionary only if the given key isn't present:
filled_dict = {"one": 1, "two": 2, "three": 3}
print(filled_dict.setdefault("five", 5))
print(filled_dict.setdefault("five", 6))
5
5
Adding to a dictionary:
filled_dict = {"one": 1, "two": 2, "three": 3}
filled_dict.update({"four":4})
print(filled_dict)
filled_dict["four"] = 4
print(filled_dict)
{'one': 1, 'two': 2, 'three': 3, 'four': 4}
{'one': 1, 'two': 2, 'three': 3, 'four': 4}
Remove keys from a dictionary with del:
filled_dict = {"one": 1, "two": 2, "three": 3}
del filled_dict["one"]
print(filled_dict)
{'two': 2, 'three': 3}
You can also use the additional unpacking options:
filled_dict = {"one": 1, "two": 2, "three": 3}
print({"a": 1, **{"b": 2}})
print({"a": 1, **{"a": 2}})
{'a': 1, 'b': 2}
{'a': 2}
Sets store ... well sets (collections of unique elements):
empty_set = set()
some_set = {1, 1, 2, 2, 3, 4}
print(empty_set)
print(some_set)
set()
{1, 2, 3, 4}
Similar to keys of a dictionary, elements of a set have to be immutable:
valid_set = {(1,), 1}
print(valid_set)
# Uncommenting the next line will raise a TypeError
# invalid_set = {[1], 1}
{(1,), 1}
Add one more item to the set:
filled_set = {1, 1, 2, 2, 3, 4}
filled_set.add(5)
print(filled_set)
filled_set.add(5)
print(filled_set)
{1, 2, 3, 4, 5}
{1, 2, 3, 4, 5}
Do set intersection with &:
filled_set = {1, 1, 2, 2, 3, 4}
other_set = {3, 4, 5, 6}
print(filled_set & other_set)
{3, 4}
Do set union with |:
filled_set = {1, 1, 2, 2, 3, 4}
other_set = {3, 4, 5, 6}
print(filled_set | other_set)
{1, 2, 3, 4, 5, 6}
Do set difference with -:
print({1, 2, 3, 4} - {2, 3, 5})
{1, 4}
Do set symmetric difference with ^:
print({1, 2, 3, 4} ^ {2, 3, 5})
{1, 4, 5}
Check if set on the left is a superset of set on the right:
print({1, 2} >= {1, 2, 3})
False
Check if set on the left is a subset of set on the right:
print({1, 2} <= {1, 2, 3})
True
Check for existence in a set with in:
filled_set = {1, 1, 2, 2, 3, 4}
print(2 in filled_set)
print(10 in filled_set)
True
False
Make a one layer deep copy:
some_set = {1, 1, 2, 2, 3, 4}
filled_set = some_set.copy()
print(filled_set)
print(filled_set is some_set)
{1, 2, 3, 4}
False
3. Control Flow and Iterables
Here is an if statement. Indentation is significant in Python! Convention is to use four spaces, not tabs. This prints "some_var is smaller than 10":
some_var = 5
if some_var > 10:
print("some_var is totally bigger than 10")
elif some_var < 10:
print("some_var is smaller than 10")
else:
print("some_var is indeed 10")
some_var is smaller than 10
For loops iterate over lists:
for animal in ["dog", "cat", "mouse"]:
print(f"{animal} is a mammal")
dog is a mammal
cat is a mammal
mouse is a mammal
"range(number)" returns an iterable of numbers from zero up to (but excluding) the given number:
for i in range(4):
print(i)
0
1
2
3
"range(lower, upper)" returns an iterable of numbers from the lower number to the upper number:
for i in range(4, 8):
print(i)
4
5
6
7
"range(lower, upper, step)" returns an iterable of numbers from the lower number to the upper number, while incrementing by step. If step is not indicated, the default value is 1:
for i in range(4, 8, 2):
print(i)
4
6
Loop over a list to retrieve both the index and the value of each list item:
animals = ["dog", "cat", "mouse"]
for i, value in enumerate(animals):
print(i, value)
0 dog
1 cat
2 mouse
While loops go until a condition is no longer met:
x = 0
while x < 4:
print(x)
x += 1
0
1
2
3
Handle exceptions with a try/except block:
try:
# Use "raise" to raise an error
raise IndexError("This is an index error")
except IndexError as e:
# Refrain from this, provide a recovery (next example).
pass
except (TypeError, NameError):
# Multiple exceptions can be processed jointly.
pass
else:
# Optional clause to the try/except block.
# Must follow all except blocks.
# Runs only if the code in try raises no exceptions.
print("All good!")
finally:
# Execute under all circumstances.
print("We can clean up resources here")
We can clean up resources here
Instead of try/finally to cleanup resources you can use a with statement:
with open("/etc/hosts") as f:
for line in f:
print(line, end="")
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
Writing to a file:
contents = {"aa": 12, "bb": 21}
with open("/tmp/myfile1.txt", "w") as file:
# Writes a string to a file.
file.write(str(contents))
import json
with open("/tmp/myfile2.txt", "w") as file:
# Writes an object to a file.
file.write(json.dumps(contents))
ok
Reading from a file:
with open("/tmp/myfile1.txt") as file:
# Reads a string from a file.
contents = file.read()
print(contents)
import json
with open("/tmp/myfile2.txt", "r") as file:
# Reads a json object from a file.
contents = json.load(file)
print(contents)
{'aa': 12, 'bb': 21}
{'aa': 12, 'bb': 21}
Python offers a fundamental abstraction called the Iterable. An iterable is an object that can be treated as a sequence:
filled_dict = {"one": 1, "two": 2, "three": 3}
our_iterable = filled_dict.keys()
ok
The object returned by the range function, is an iterable.
We can loop over it:
for i in our_iterable:
print(i)
# However we cannot address elements by index.
# our_iterable[1] # Raises a TypeError
one
two
three
An iterable is an object that knows how to create an iterator:
our_iterator = iter(our_iterable)
# Our iterator is an object that can remember the state
# as we traverse through it. We get the next object with "next()".
print(next(our_iterator))
# It maintains state as we iterate.
print(next(our_iterator))
print(next(our_iterator))
# After the iterator has returned all of its data,
# it raises a StopIteration exception.
# print(next(our_iterator))
one
two
three
We can also loop over an iterator. In fact, "for" does this implicitly:
our_iterator = iter(our_iterable)
for i in our_iterator:
print(i)
one
two
three
You can grab all the elements of an iterable or iterator by call of list():
print(list(our_iterable))
our_iterator = iter(our_iterable)
print(list(our_iterator))
['one', 'two', 'three']
['one', 'two', 'three']
4. Functions
Use "def" to create new functions:
def add(x, y):
print(f"x is {x} and y is {y}")
return x + y
ok
Calling functions with parameters:
add(5, 6)
x is 5 and y is 6
Another way to call functions is with keyword arguments. They can arrive in any order:
add(y=6, x=5)
x is 5 and y is 6
You can define functions that take a variable number of positional arguments:
def varargs(*args):
return args
print(varargs(1, 2, 3))
(1, 2, 3)
You can define functions that take a variable number of keyword arguments:
def keyword_args(**kwargs):
return kwargs
print(keyword_args(big="foot", loch="ness"))
{'big': 'foot', 'loch': 'ness'}
You can do both at once, if you like:
def all_the_args(*args, **kwargs):
print(args)
print(kwargs)
all_the_args(1, 2, a=3, b=4)
(1, 2)
{'a': 3, 'b': 4}
When calling functions, you can do the opposite of args/kwargs. Use * to expand args (tuples) and use ** to expand kwargs (dictionaries):
def all_the_args(*args, **kwargs):
print(args)
print(kwargs)
args = (1, 2, 3, 4)
kwargs = {"a": 3, "b": 4}
all_the_args(*args) # same as all_the_args(1, 2, 3, 4)
all_the_args(**kwargs) # same as all_the_args(a=3, b=4)
all_the_args(*args, **kwargs) # same as all_the_args(1, 2, 3, 4, a=3, b=4)
(1, 2, 3, 4)
{}
()
{'a': 3, 'b': 4}
(1, 2, 3, 4)
{'a': 3, 'b': 4}
Returning multiple values (with tuple assignments):
def swap(x, y):
return y, x
x = 1
y = 2
x, y = swap(x, y) # same as (x, y) = swap(x,y)
print(x, y)
2 1
Global scope:
x = 5
def set_x(num):
# local scope begins here
# local var x not the same as global var x
x = num
print(x)
def set_global_x(num):
# global indicates that particular var lives in the global scope
global x
print(x)
x = num # global var x is now set to 6
print(x)
set_x(43)
set_global_x(6)
43
5
6
Python has first class functions:
def create_adder(x):
def adder(y):
return x + y
return adder
add_10 = create_adder(10)
print(add_10(3))
13
Closures in nested functions. We can use the nonlocal keyword to work with variables in nested scope which shouldn't be declared in the inner functions:
def create_avg():
total = 0
count = 0
def avg(n):
nonlocal total, count
total += n
count += 1
return total/count
return avg
avg = create_avg()
print(avg(3))
print(avg(5))
print(avg(7))
3.0
4.0
5.0
There are also anonymous functions:
print((lambda x: x > 2)(3))
print((lambda x, y: x ** 2 + y ** 2)(2, 1))
True
5
There are built-in higher order functions "map" and "filter":
def add_10(x):
return x + 10
print(list(map(add_10, [1, 2, 3])))
print(list(map(max, [1, 2, 3], [4, 2, 1])))
print(list(filter(lambda x: x > 5, [3, 4, 5, 6, 7])))
[11, 12, 13]
[4, 2, 3]
[6, 7]
We can use list comprehensions for nice maps and filters. List comprehension stores the output as a list (which itself may be nested):
def add_10(x):
return x + 10
print([add_10(i) for i in [1, 2, 3]])
print([x for x in [3, 4, 5, 6, 7] if x > 5])
[11, 12, 13]
[6, 7]
You can construct set and dict comprehensions as well:
print({x for x in "abcddeef" if x not in "abc"})
print({x: x**2 for x in range(5)})
{'d', 'f', 'e'}
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
5. Modules
You can import modules:
import math
print(math.sqrt(16))
4.0
You can get specific functions from a module:
from math import ceil, floor
print(ceil(3.7))
print(floor(3.7))
4
3
You can import all functions from a module (not recommended):
from math import *
print(sqrt(16))
4.0
You can shorten module names:
import math as m
print(m.sqrt(16))
4.0
Python modules are just ordinary Python files. You can write your own, and import them. The name of the module is the same as the name of the file.
You can find out which functions and attributes are defined in a module:
import math
print(dir(math))
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'cbrt', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'exp2', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'nextafter', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'sumprod', 'tan', 'tanh', 'tau', 'trunc', 'ulp']
If you have a Python script named math.py in the same folder as your current script, the file math.py will be loaded instead of the built-in Python module. This happens because the local folder has priority over Python's built-in libraries.
6. Classes
We use the "class" statement to create a class:
class Human:
# A class attribute. It is shared by all instances of this class
species = "H. sapiens"
# Basic initializer, this is called when this class is instantiated.
# Methods with double underscores (__init__, __str__, __repr__ etc.)
# are called special methods (or sometimes called dunder
# methods). You should not invent such names on your own.
def __init__(self, name):
# Assign the argument to the instance's name attribute
self.name = name
# Initialize property
self._age = 0
# The leading underscore indicates the "age" property is private.
# Do not rely on this to be enforced: it's a hint to other devs.
# An instance method. All methods take "self" as the first argument
def say(self, msg):
print("{name}: {message}".format(name=self.name, message=msg))
# Another instance method
def sing(self):
return "yo... yo... microphone check... one two... one two..."
# A class method is shared among all instances
# They are called with the calling class as the first argument
@classmethod
def get_species(cls):
return cls.species
# A static method is called without a class or instance reference
@staticmethod
def grunt():
return "*grunt*"
# A property is just like a getter.
# It turns the method age() into a read-only attribute of the same name.
# There's no need to write trivial getters and setters in Python, though.
@property
def age(self):
return self._age
# This allows the property to be set
@age.setter
def age(self, age):
self._age = age
# This allows the property to be deleted
@age.deleter
def age(self):
del self._age
ok
When a Python interpreter reads a source file it executes all its code. This __name__ check makes sure this code block is only executed when this module is the main program:
if __name__ == "__main__":
# Instantiate a class
inga = Human(name="Inga")
inga.say("hi")
joel = Human("Joel")
joel.say("hello")
Inga: hi
Joel: hello
# inga and joel are instances of type Human;
# i.e., they are Human objects.
inga = Human(name="Inga")
joel = Human("Joel")
ok
Call our class method:
inga.say(inga.get_species())
Inga: H. sapiens
Change the shared attribute:
Human.species = "H. neanderthalensis"
inga.say(inga.get_species())
joel.say(joel.get_species())
Inga: H. neanderthalensis
Joel: H. neanderthalensis
Call the static method:
print(Human.grunt())
# Static methods can be called by instances too.
print(inga.grunt())
*grunt*
*grunt*
Work with properties:
# Update the property for this instance.
inga.age = 42
# Get the property
inga.say(inga.age)
joel.say(joel.age)
# Delete the property
del inga.age
# inga.age # this would raise an AttributeError
Inga: 42
Joel: 0
6.1 Inheritance
Inheritance allows new child classes to be defined that inherit methods and variables from their parent class.
Using the Animal class as the base or parent class, we can define a child class, Dog, which inherits variables like "name", as well as methods like "speak" from the Animal class, but can also have its own unique properties.
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return f"{self.name} makes a sound."
class Dog(Animal):
# Children automatically inherit their parent class's
# constructor, but can also define their own.
# This constructor adds the "barks" argument:
def __init__(self, name, barks=True):
self.barks = barks
# Use "super" to access the parent class's methods.
super().__init__(name)
def speak(self):
if self.barks:
return f"{self.name} barks!"
return super().speak()
ok
dog = Dog("Buddy")
print(dog.speak())
dog = Dog("Weirdo", barks=False)
print(dog.speak())
Buddy barks!
Weirdo makes a sound.
6.2 Multiple Inheritance
You can inherit from multiple classes. For example, use Superhero and Bat as bases for Batman:
class Superhero:
species = "Superhuman"
def __init__(self, name, movie=False, superpowers=None):
self.name = name
self.movie = movie
self.superpowers = superpowers or ["super strength", "bulletproofing"]
def say(self, msg):
print(f"{self.name}: {msg}")
def get_species(self):
return self.species
def sing(self):
return "Dun, dun, DUN!"
ok
class Bat:
species = "Baty"
def __init__(self, can_fly=True):
self.fly = can_fly
# Bat also has a say method
def say(self, msg):
msg = "... ... ..."
return msg
# And its own method as well
def sonar(self):
return "))) ... ((("
ok
Define Batman as a child that inherits from both Superhero and Bat:
class Batman(Superhero, Bat):
species = "Human"
def __init__(self, *args, **kwargs):
# Typically to inherit attributes you have to call super:
# super(Batman, self).__init__(*args, **kwargs).
# However we are dealing with multiple inheritance here, and super()
# only works with the next base class in the MRO list.
# So instead we explicitly call __init__ for all ancestors.
# The use of *args and **kwargs allows for a clean way to pass
# arguments, with each parent "peeling a layer of the onion".
Superhero.__init__(self, "anonymous", movie=True,
superpowers=["Wealthy"], *args, **kwargs)
Bat.__init__(self, *args, can_fly=False, **kwargs)
# override the value for the name attribute
self.name = "Sad Affleck"
def sing(self):
return "nan nan nan nan nan batman!"
ok
The Method Resolution Order:
sup = Batman()
print(Batman.__mro__)
(<class '__main__.Batman'>, <class '__main__.Superhero'>, <class '__main__.Bat'>, <class 'object'>)
Calls parent method (get_species) but uses its own class attribute (species):
sup = Batman()
print(sup.get_species())
Human
Calls overridden method:
sup = Batman()
print(sup.sing())
nan nan nan nan nan batman!
Calls method from the 1st ancestor (Human), because inheritance order matters:
sup = Batman()
sup.say("I agree")
Sad Affleck: I agree
Call method that exists only in 2nd ancestor (Bat):
sup = Batman()
print(sup.sonar())
))) ... (((
Inherited class attribute:
sup = Batman()
sup.age = 100
print(sup.age)
100
Inherited attribute from 2nd ancestor (Bat) whose default value was overridden:
sup = Batman()
print(f"Can I fly? {sup.fly}")
Can I fly? False
7. Generators and Decorators
A generator in Python is a special type of iterator that allows you to iterate over a sequence of values without storing the entire sequence in memory. Generators are created using functions and the "yield" keyword.
Generators help you make lazy code:
def double_numbers(iterable):
for i in iterable:
yield i + i
ok
Generators are memory-efficient because they only load the data needed to process the next value in the iterable. This allows them to perform operations on otherwise prohibitively large value ranges:
for i in double_numbers(range(1, 900000000)):
print(i)
if i >= 10:
break
2
4
6
8
10
Just as you can create a list comprehension, you can create generator comprehensions as well:
values = (-x for x in [1, 2, 3, 4, 5])
for x in values:
print(x)
-1
-2
-3
-4
-5
You can also cast a generator comprehension directly to a list:
values = (-x for x in [1, 2, 3, 4, 5])
gen_to_list = list(values)
print(gen_to_list)
[-1, -2, -3, -4, -5]
A decorator in Python is a design pattern that allows you to modify or extend the behavior of a function or method without changing its actual code.
Decorators are a form of syntactic sugar:
def log_function(func):
def wrapper(*args, **kwargs):
print(f"Entering function {func.__name__}")
result = func(*args, **kwargs)
print(f"Exiting function {func.__name__}")
return result
return wrapper
@log_function
def my_function(x, y):
"""Sums two arguments and returns the result."""
return x+y
ok
The decorator @log_function tells us as we begin reading the function definition for my_function that this function will be wrapped with log_function (similar to my_function = log_function(my_function)).
Calling my_function now calls the wrapped function:
my_function(1, 2)
Entering function my_function
Exiting function my_function
But there's a problem. What happens if we try to get some information about my_function?
print(my_function.__name__)
print(my_function.__doc__)
wrapper
None
We've replaced information about my_function with information from wrapper. The name and the docstring are lost.
Fix this using functools:
from functools import wraps
def log_function(func):
@wraps(func)
def wrapper(*args, **kwargs):
print("Entering function", func.__name__)
result = func(*args, **kwargs)
print("Exiting function", func.__name__)
return result
return wrapper
@log_function
def my_function(x, y):
"""Sums two arguments and returns the result."""
return x+y
ok
The @wraps decorator ensures that docstring, function name and some other attributes are copied to the wrapped function - instead of being replaced with wrapper's info:
print(my_function.__name__)
print(my_function.__doc__)
my_function
Sums two arguments and returns the result.
Further Reading
- Official Docs
- Official Style Guide for Python
- Automate the Boring Stuff with Python
- Hitchhiker's Guide to Python
- Python Course
- First Steps With Python
- Python Exercises
- Build a Desktop App with Python
- Curated list of Python stuff
efemeral-net + 8 others · original · CC-BY-SA 3.0 · 2025-03-08
efemeral-net, Louie Dinh, Zachary Ferguson, evuez, Rommel Martinez, Roberto Fernandez Diaz, caminsha, Stanislav Modrak, John Paul Wohlscheid