Skip to content

Control flows

Conditional flows

Action Code Details
If statement
if a == 2:
    print('A is 2')
If statement on single line (one-liner)
if x < y < z: print(x); print(y); print(z)
If-else statement
if a == 2:
    print('A is 2')
else:
    print('A is not 2')
Nested if-else statements
if a == 2:
    print('A is 2')
elif a == 3:
    print('A is 3')
else:
    print('A is not 2 and 3')
Switch statement (match)
match fruit:
    case 'banana':
        print('Fruit is banana')
    case 'apple':
        print('Apple')
Switch statement with default
match fruit:
    case 'banana':
        print('Fruit is banana')
    case 'apple':
        print('Apple')
    case _:  # default
        print('Undefined object')
Switch statement with conditional (guarded) cases
match fruit:
    case 'banana':
        print('Banana')
    case 'apple' if in_stock > 5:
        print('Apple available')
    case _:  # default
        print('Unavailable')

Iterative flows

Action Code Details
While loop
while condition():
    work()
Infinite while loop
while True:
    pass
While loop with skips
while condition():
    if condition2():
        continue
    work()
While loop with finalization
while condition():
    work()
else:
    print('Work completed')
Do-while loop
while True:
    work()
    if condition():
        break
Not natively supported in Python. This snippet does not work with Else because the loop is terminated using break.
For loop
for i in range(4):
    work(i)
For loop with finalization
for i in range(4):
    work(i)
else:
    print('Success')
For loop with skips
for i in range(4):
    if i == 2:
        continue
    work(i)

Error/exception flows

Action Code Details
Assert statement
assert True != False
Assert statement with custom message
assert True != False, 'faulty logic'
Raise error e
raise e
Try statement
try:
    io_action()
Try statement with general exception handling
import sys
try:
    io_action()
except:
    print(repr(sys.exception()))
Try statement with exception handling
try:
    io_action()
except e as FileNotFoundError:
    print(repr(sys.exception()))
Try statement with handling of multiple exceptions
try:
    io_action()
except e as FileNotFoundError:
    print('File is missing')
except e as InterruptedError:
    print('Interrupted')
Try statement with exception group handling
try:
    io_action()
except* OSError as e:
    print(f'caught {type(e)} with nested {e.exceptions}')
Note the asterisk
Try statement with finalization when executation completed successfully (no errors, returns, breaks)
try:
    io_action()
else:
    print('Success!')
Try statement with cleanup statement
try:
    io_action()
finally:
    print('We are done')
The finally clause is always executed, even when the try statement errs, returns, or breaks.
With statement
with a() as A:
    work()
Run statements using a context manager, which ensures safe handling of resources, handling both opening (enter) and closing (exit).
Multiple with statements
with a() as A, b() as B:
    work()