Module lessons (3/4)
Conditionals: if, elif, else
Conditional structures allow the program to choose which branch to execute based on the current state. In Python, indentation is not cosmetic: it replaces curly braces and physically delimits which lines belong to each branch.
The mental model: mutatively exclusive decision tree
When Python enters a if -> elif -> else chain, it evaluates a clause from top to bottom and stops at the first true branch. If two conditions overlap, the one written higher always wins!
| Constructed | Syntactic form | When to use it | Return value |
|---|---|---|---|
| Basic block | if cond: ... else: ... | Multiple actions or side effects | None (education) |
| Branch chain | if c1: ... elif c2: ... else: ... | Unique thresholds or multi-level classification | None (education) |
| Ternary expression | val_a if cond else val_b | Choose between two short values on one line | Returns a value |
Indentation and branch order elif
# Esempio 1: ramo base con indentazione coerente a 4 spazi
user_age = 18
if user_age >= 18:
status = "adult"
else:
status = "minor"
# Esempio 2: attenzione all'ordine delle soglie (dalla più restrittiva alla più ampia!)
score = 92
if score >= 90:
grade = "A"
elif score >= 75:
grade = "B"
elif score >= 60:
grade = "C"
else:
grade = "F"truthy/falsy values and ternary expression
Any object can come after if: only False, None, zeros (0, 0.0) and empty containers ("", [], {}, set() are false). Everything else is truthy.
# Esempio 3: controllo idiomatico di contenitore non vuoto (senza len(items) > 0)
pending_tasks = ["sync", "backup"]
if pending_tasks:
queue_state = f"ready ({len(pending_tasks)})"
else:
queue_state = "idle"
# Esempio 4: espressione condizionale ternaria (sempre con else esplicito!)
items_count = 1
suffix = "item" if items_count == 1 else "items"Quick check
What does the tier variable at the end of this snippet contain?
score = 95
if score >= 60:
tier = "bronze"
elif score >= 90:
tier = "gold"
else:
tier = "retry"What value is considered truthy in Python?
Test yourself
Step 1 — Three-way classification with elif
Use if / elif / else to distinguish positive, zero and negative numbers.
Given number = 15, assign sign the string 'positive' if number > 0, 'zero' if == 0, or 'negative' otherwise. Currency sign.
Show hint
Handle number > 0 first, then elif number == 0:, finally else:.
Solution available after 3 attempts
Step 2 — Ternary conditional expression
Assigns the result of a binary choice in a single line.
Given age = 20, assign category the string 'adult' if age >= 18 else 'minor' using the one-line ternary construct.
Show hint
The ternary syntax puts the true result first, then if condition else false_result.
Solution available after 3 attempts
Step 3 — Debugging a complete conditional chain
Test the three branches on a negative number while keeping the logical order clean.
Starting with x = -5, set sign to 'positive' (x > 0), 'negative' (x < 0), or 'zero' with if / elif / else, then evaluate sign.
Show hint
Use if x > 0:, elif x < 0: and else:.
Solution available after 3 attempts