Guide Book · Chapter 9
Control flow
if/elif/else, while, for, break/continue, loop-else and match, and how loops really iterate.
if / elif / else
python
temp = 23
if temp > 30:
label = "hot"
elif temp > 15:
label = "mild"
else:
label = "cold"
print(label, "|", "warm" if temp > 20 else "cool")Conditions use truthiness: 0, 0.0, "", None, and empty [] {} () set() are falsy; everything else is truthy.
python
for value in [0, 1, "", "a", [], [0], None]:
print(repr(value), "→", bool(value))while
python
n, steps = 27, 0
while n != 1: # the Collatz sequence
n = n // 2 if n % 2 == 0 else 3 * n + 1
steps += 1
print(steps)for, range, enumerate, zip
python
for i in range(2, 11, 4):
print(i)
for i, ch in enumerate("abc", start=1):
print(i, ch)
for name, score in zip(["Ada", "Bo"], [90, 80], strict=True):
print(name, score)python
things = ["a", "b", "c"]
it = iter(things)
while True:
try:
x = next(it)
except StopIteration:
break
print(x)python
import sys r = range(10**9) print(sys.getsizeof(r), len(r), 999_999_999 in r, r[123]) print(sys.maxsize) # 2147483647 here: this Python is a 32-bit WebAssembly build
break, continue and loop-else
else on a loop runs only if the loop wasn't broken out of, which is perfect for searches.
python
def find_even(nums):
for n in nums:
if n % 2:
continue # skip odd numbers
print("found", n)
break
else:
print("no even number")
find_even([1, 3, 4, 5])
find_even([1, 3])match (structural pattern matching)
python
def describe(event):
match event:
case {"type": "click", "x": x, "y": y}:
return f"click at {x},{y}"
case {"type": "key", "key": "q" | "Q"}:
return "quit"
case [first, *rest]:
return f"batch of {1 + len(rest)} starting with {first}"
case str() as text if text:
return f"text: {text}"
case _:
return "unknown"
for e in [{"type": "click", "x": 1, "y": 2}, {"type": "key", "key": "Q"}, [1, 2, 3], "hi", 42]:
print(describe(e))⚠️ Gotcha: don't modify a list while looping over it; items get skipped. Loop over a copy, or build a new list.
python
nums = [1, 2, 2, 3]
for n in nums[:]: # iterate over a copy
if n == 2:
nums.remove(n)
print(nums)
print([n for n in [1, 2, 2, 3] if n != 2])🧭 Scenario: Retry with a limit: loop until success or out of attempts, and use else to report failure only when every attempt ran.
python
import random
random.seed(3)
for attempt in range(1, 4):
ok = random.random() > 0.7
print(f"attempt {attempt}: {'ok' if ok else 'failed'}")
if ok:
break
else:
print("giving up after 3 attempts")