Cheat Sheets

A collection of cheat sheets.

Hosted on GitHub Pages — Theme by mattgraham

Flow Control

Conditional

# If statement
if cond:
    ...
elif cond:
    ...
else:
    ...

# If expression
result = a if cond else b

Loops

while cond:
    if cond:
        continue # Jump back to beginning of loop
    if cond:
        break # Escape the loop
for x in xs:
    ...

Examples

# Repeat 10 times
for _ in range(10):
    ...

# Iterate over list
for x in [1, 2, 3, 4]:
    ...

# Iterate over dictionary
for key, value in dictionary.items():
    ...