2.3 Control Flow: if / elif / else, Truthy/Falsy & match-case
Teach your Python programs to make smart decisions! Learn branching flowcharts, the 7 Falsy values every Python dev memorizes, clean ternary expressions, and modern match-case.
1. Making Decisions: The Traffic-Light Analogy
Without conditionals, a program runs the exact same lines every time like a train on a single track. if / elif / else adds railway switches so your code can take different paths depending on the data.
Key rule: In an if / elif / else chain, Python checks conditions from top to bottom and executes ONLY the first branch that is True — then skips the rest!
score = 82
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
else:
grade = "Need Practice"
print(f"Score {score} -> Grade {grade}") # Score 82 -> Grade B
Beginner Trap: if + if vs. if + elif
If you accidentally write two separate if statements instead of if / elif, both can run!
- Use
elifwhen the choices are mutually exclusive (a student can only have one letter grade). - Use separate
ifstatements when multiple independent checks should run (e.g., check if a password is long enough and check if it has a digit).
2. Truthy & Falsy Values (Writing Clean, Pythonic if Checks)
In Python, you don't have to write if len(items) > 0: or if name != "":. Every object in Python automatically counts as either Truthy or Falsy inside an if statement!
Just memorize the 7 Falsy values below — literally everything else is Truthy:
cart = []
# ✗ Clunky (Java/C style)
if len(cart) == 0:
print("Your cart is empty")
# ✓ Pythonic! Empty list [] is Falsy, so `not cart` is True
if not cart:
print("Your cart is empty")
3. One-Line Conditional (Ternary Expression)
When you just want to pick between two simple values based on a condition, Python offers a clean 1-line conditional expression:
age = 20
ticket_type = "Adult" if age >= 18 else "Child"
print(ticket_type) # Adult
Readability Rule: Only use ternary expressions for short, simple choices. Never nest a ternary inside another ternary!
4. Modern Python match-case (Python 3.10+)
If you have many exact options (like HTTP status codes or CLI commands), match-case is much cleaner than a long ladder of elifs. Use _ as the default fallback ("anything else"):
status_code = 404
match status_code:
case 200 | 201:
print("Success!")
case 400:
print("Bad Request")
case 404:
print("Page Not Found")
case 500 | 502 | 503:
print("Server Error — Retry")
case _:
print(f"Unhandled status: {status_code}")
(Unlike C/Java switch, Python match-case does not fall through — you never need break!)
5. Interactive Trace: Guard Clauses & Nested Conditionals
Pro developers avoid deeply nested if pyramids by using early checks. Step through this shipping-fee calculator:
Quick Quiz: Module 2.3 — Control Flow & Truthy/Falsy
x = 15
if x > 5:
print("A")
elif x > 10:
print("B")Keep Exploring: Related Lessons & Tools
All Courses →Paste any Python, JS, Java, or C++ snippet to estimate Time & Space complexity, or enter input size N to see which algorithms pass in 1s.
Test regular expressions live with color-coded match highlights, capture group inspection, plain-English token breakdown, find-and-replace, and Python/JS snippets.