What you'll learn
So far programs run straight through. Real programs make decisions — doing different things for different inputs. This module is all about control flow in Python: if statements, the ternary expression, and modern match statements.
By the end you'll be able to:
- Build conditions and understand Python's truthiness
- Branch with
if,elif, andelse - Write compact choices with the ternary expression
- Match against patterns with
match/case - Avoid the classic control-flow mistakes
Booleans & truthiness
Every decision rests on a value that's True or False. Comparison operators (Module 2) produce booleans directly. But Python goes further: any value can be tested for truth. This is called truthiness, and it makes code beautifully concise:
Falsy — treated as False
Truthy — treated as True
Essentially everything else: any non-zero number, any non-empty string, list, or dictionary.
name = ""
items = [1, 2, 3]
if name: # empty string is falsy
print("Has a name")
else:
print("No name given")
if items: # non-empty list is truthy
print(f"{len(items)} items")Tip
if items: rather than if len(items) != 0:. It reads like plain English — "if there are items" — and it's the Pythonic way.if, elif & else
The if statement runs a block only when its condition is true. Add elif ("else if") to test more conditions in order, and else for the fallback. Two Python essentials: the condition ends with a colon :, and the block is defined by indentation — no braces:
score = 82
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
else:
print("Grade: F")Note
if, then each elif in order, and stops at the first true one — the rest are skipped.The ternary expression
When you just need to pick between two values, Python's ternary reads almost like English: value_if_true if condition else value_if_false:
age = 20
status = "adult" if age >= 18 else "minor"
print(status)
a, b = 7, 12
print("bigger:", a if a > b else b)match / case
Introduced in Python 3.10, match compares a value against several patterns — cleaner than a long elif chain when checking one value against many options. The underscore _ is the wildcard that matches anything (the default):
command = "start"
match command:
case "start":
print("Starting up...")
case "stop":
print("Shutting down...")
case _: # _ is the wildcard (default)
print("Unknown command")Note
match can do far more than compare literals — it can destructure lists, tuples, and objects (structural pattern matching). For now, just know it's the modern, readable choice for multi-way branching on a single value.Common mistakes
- Forgetting the colon — every
if,elif, andelseline ends with:. - Inconsistent indentation — mixing tabs and spaces, or the wrong amount, is a real error in Python.
- Using
=instead of==—=assigns,==compares. (Python catches this in anif, unlike some languages.)
== vs is
== checks whether two values are equal; is checks whether they're the exact same object. Use == for values, and reserve is for None: write if x is None:.Recap & quick check
Key takeaways
- Conditions are booleans, but ANY value is truthy or falsy — empty/zero/None are falsy.
- if / elif / else branch in order; the first true branch wins. Use a colon and indentation, not braces.
- Write 'if items:' rather than 'if len(items) != 0:' — it's the Pythonic way.
- The ternary 'a if condition else b' picks between two values in one line.
- match / case (3.10+) is clean multi-way branching; use 'is None', and == (not =) to compare.
Quick check
1. What ends an if/elif/else line in Python?
2. Which of these is falsy in Python?
3. How many branches run in an if/elif/else chain?
4. How should you check if a variable x is None?
5. Which statement is best for matching one value against many options (Python 3.10+)?
Nicely done — your code can now think for itself. Next up: Module 6 — Loops, where you'll make code repeat work automatically.