QuickDevGuide

Home › Python

Control Flow & Loops — Python

Conditionals if / elif / else with no braces — indentation defines blocks. The conditional expression is a if cond else b. Structural pattern matching (3.10+) match / case matches shapes of data: literals, sequences, dicts, classes, with alternatives and if guards. It is far more than a switch statement. Loops for item in iterable: — iterates any iterable. enumerate(seq, start=1) — index + value. zip(a, b) — iterate in parallel (strict=True in 3.10+ to catch length mismatches). range(start, stop, step) — lazy integer sequence. while cond: with break / continue. for…else / while…else — the else runs only if the loop did not break (handy for searches). Walrus operator (3.8+) if (n := len(data)) > 10: assigns and tests in one expression.

Key Takeaways