Show Menu
Cheatography

pythomaniac anew Cheat Sheet (DRAFT) by

In depth python cheat sheet

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Functions

def myfunc­­ti­o­n­(f­­irst: int, second: str) -> float:
sum: Callab­­le­[­[int, int], int] = lambda x, y: x + y
Lambda function
def foo(first: int, *rest: tuple) -> None:
Variable function args (rest is a tuple). Calling: foo(1, 2, 3)
def foo(first: int, **options: dict) -> None:
Variable function args (options is a dict). Call as foo(1, sec=2, third=3)

Decorators

def simple_decorator(func):
    def wrapper(args, *kwargs):
        print("Before the function call")
        func(args, *kwargs)
        print("After the function call")
    return wrapper

@simple_decorator
def greet(name: str):
    print(f"Hello there {name}!")

greet("King")

# @decorator replaces this:
decorate = simple_decorator(greet)
decorate("King")
A decorator is a design pattern in Python that allows a user to add new functi­­on­ality to an existing object without modifying its structure. Decorators are typically applied to functions, and they play a crucial role in enhancing or modifying the behavior of functions.

When you create a decorator, the wrapper function (inside the decorator) is a closure. It retains access to the function being decorated and any additional state or arguments defined in the decorator function.

Loops

for x in range(­[start, ] end [, step]):
end, step optional. Range = [start, end[
break, continue
Loop control
while, for:
  ...
else:
Altern­­ative if loop condition false from beginning

Lists

mylist = []
Initia­liz­ation
.ap­pend()
Add element to list
for x in mylist:
Foreach
if elem in mylist:
Check if element in list
mylist­­[s­t­a­rt­­:st­­op­:­step]
List slice (all parts optional)
lengths = [len(w) for w in words if w != "­ski­p"]
List compre­­he­nsion

Sets

a = { "­a­", "­­b" }
Initia­liz­ation
a.inte­­rs­e­c­ti­­on(b)
Elements in a and b
a.diff­­er­e­n­ce(b)
Elements in a but not in b
a.union(b)
All elements
Sets do not have duplicate entries.
Allow for easy combine operat­ions.

Closures

from typing import Callable

def outer_func(message: str) -> Callable[[], None]:
    def inner_func() -> None:
        print(message)
    return inner_func

# Calling
do_print = outer_func("Hello world")
print("Now we actually print ...")
do_print()
While the function outer_­­fu­n­c­tion() completed, the message was rather preserved but hidden and attached to the code.
Decorators make heavy use of this.

Type Annota­tions

from typing import Callable
from typing import Generator
None | float
Multiple possible types
Python is both a strongly typed and a dynami­­cally typed language. Strong typing means that variables do have a type and that the type matters when performing operations on a variable. Dynamic typing means that the type of the variable is determined only during runtime.

Adding type annota­tions is a huge advantage for anyone using your modules.

Modules

import my_module
Expects a my_mod­­ul­e.py. Usage: my_mod­­ul­e.s­o­m­e_­­func()
from my_module import some_func
Import specific from module. Usage: my_func()
from my_module import *
Not recomm­­ended. Imports everyt­hing.
import my_module as mine
Alias. Usage: mine.m­­y_­f­unc()
Each .py file is considered a module

Modules are initia­lized only once at first encoun­tered import. If imported again somewhere else, it will not be loaded anew. This is why local variables act as single­­tons.

Data Types

int, float, str
Basic data types
True, False
Boolean
[1, 2, 3]
List
("he­llo­", "­wor­ld")
Tuple
{ "­key­": "­val­ue" }
Dictionary
{ "­fir­st", "­sec­ond­" }
Set 1
1 Create an empty set with set()

Packages

Needs __init­­__.py file
__all__ = [ "­­pu­b­l­ic­­_mo­­du­l­e­" ]
Override exports and keep certain modules internal
A directory that get's its own namespace containing modules and other packages.

Classes

class Dude:
    def __init__(self, name: str) -> None:
        self.name = name

    def introduce(self) -> str:
        return f"Hello, I am {self.name}"

my_object = Dude("ThaDude")
print(my_object.introduce())

Debugging

s
Execute the current line, stop at the first possible occasion
c
Continue execution, only stop when a breakpoint is encoun­­tered.
h
Print help
p
Print something like a variable
Just add breakp­oint() somewhere in code and when execution comes to this point you will be dropped to pdb, an intera­ctive source code debugger.

Exception Handling

try:
  pass
except Exception as e:
  pass
finally:
  pass

Leftovers

"­­", None, [] and {}
These are all consider False in a logical expression
 

Useful Packages

dacite
This module simplifies creation of data classes (PEP 557) from dictio­­na­ries.
requests
Requests is a simple, yet elegant, HTTP library.

Generators

import random
from typing import Generator

def lottery() -> Generator[int, None, None]:
    for i in range(6):
        yield random.randint(1, 43)
    yield random.randint(1, 20) # Joker

# Usage:
for number in lottery():
    print(number)
Functions that returns an iterable set of items one at a time (yield).

Nested Functions

def outer_func(message: str) -> None:
  def inner_func() -> None:
    print(message)
  
  inner_func()
Nested functions can access variables of enclosing scopes, but they are readonly. Can be circum­­vented using nonlocal keyword.

Main

def main():
  print("Hello World")

if __name__ == '__main__':
  main()
main() is only called when file is ran directly.

If the file is imported into another module, main() is not called.

Regular Expres­sions

import re
pattern = re.com­­pi­l­e­(r­­"..."­­)
Todo: add search, replace, matching, ...

Code Intros­pection

help()
Show help on class, function, method, ...
dir()
Return all the properties and methods, even built-in properties which are default for all object.
hasattr()
Returns True if the specified object has the specified attribute.
id()
Returns a unique id for the specified object.
type()
Returns the type of the specified object.
repr()
Returns the canonical string repres­ent­ation of the object.
callable()
Return whether the object is callable (i.e., some kind of function).
issubc­­lass()
Return whether 'cls' is derived from another class or is the same class.
isinst­­ance()
Return whether an object is an instance of a class or of a subclass thereof.
Remember that in python everything is an object.

JSON

import json
json_obj = json.l­­oa­d­s­(j­­son­­_s­t­ring)
Convert string to data object
json_s­­tring = json.d­­um­p­s­(j­­son­­_obj)
Convert data object to string
Python supports a similar data serial­ization method called pickle which can be used in exactly the same way.

Dictio­naries

book = { "­­he­l­l­o": 55 }
Initialize
book["t­­es­t­"] = 22
Store value at key
for key,value in book.i­­te­ms():
Foreach
del book["t­­es­t­"]
Delete without return value
elem = book.p­­op­(­"­­tes­­t")
Pop with return
if hello in book:
Check if key present
Key / Value storage. Key can also be anything.

Input / Output

print(­­"­H­ello, %s" % name)
Print formatting
print(­­f"H­ello, {name}­­")
In-place expres­­sions
astring = input()
Read till newline
anumber = int(in­­put())
Expects exactly 1 int
a, b = map(int, input(­­).s­­p­lit())
Expects exactly 2 ints
array = input(­­).s­­p­lit()

Operators

a % b
a module b
a ** b
ab
a + b
Addition, string concat, list concat
"­­5" * 3
"­­55­5­"
[5] * 3
[5, 5, 5]
a // b
Floor division
not, and, or
Logical operators
is, is not
Identity operators
in, not in
Membership operators
&, |, ~, ^, >>, <<
Bitwise operators
x if condition else y
Ternary operator
a op= b
Shortened assignment 1
1 op can basically be any operator

Instance variables versus Class variables

class Dude:
    count: int = 0

    def __init__(self, name: str) -> None:
        self.name = name
        Dude.count = Dude.count + 1

print(Dude.count)  # 0
dude = Dude("ThaDude")
print(Dude.count)  # 1
king = Dude("King")
print(Dude.count)  # 2
print(dude.count)  # 2
print(king.count)  # 2

king.count = 666
print(Dude.count)  # 2
print(dude.count)  # 2
print(king.count)  # 666
king just got an instance variable count that takes precedence over Dude.count.

When no variable can be found with the given name on the instance, a fallback happens to the class variable. However when we assign a value as in king.count = 2 we are creating an instance variable for that object. This is also called Name Shadowing

Exception Handling cont.

e.add_­­no­t­e­("Some info")
Add some extra context info to exception
raise e or raise
Reraise the exception
raise NewError from e
Chained exception; indicate that NewError was caused by e