Skip to main content
Module 1 · Lesson 3 of 43/36 in the course~14 min
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!

ConstructedSyntactic formWhen to use itReturn value
Basic blockif cond: ... else: ...Multiple actions or side effectsNone (education)
Branch chainif c1: ... elif c2: ... else: ...Unique thresholds or multi-level classificationNone (education)
Ternary expressionval_a if cond else val_bChoose between two short values on one lineReturns a value

Indentation and branch order elif

Python
# 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.

Python
# 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

Python Quiz#python.m1.l3.e4
Ready

What does the tier variable at the end of this snippet contain?

Python
score = 95
if score >= 60:
    tier = "bronze"
elif score >= 90:
    tier = "gold"
else:
    tier = "retry"
Answer options
Python Quiz#python.m1.l3.e5
Ready

What value is considered truthy in Python?

Answer options

Test yourself

Warm-Up Stage

Step 1 — Three-way classification with elif

Use if / elif / else to distinguish positive, zero and negative numbers.

Exercise#python.m1.l3.e1
Attempts: 0Loading…

Given number = 15, assign sign the string 'positive' if number > 0, 'zero' if == 0, or 'negative' otherwise. Currency sign.

Loading editor…
Show hint

Handle number > 0 first, then elif number == 0:, finally else:.

Solution available after 3 attempts

Practice Stage

Step 2 — Ternary conditional expression

Assigns the result of a binary choice in a single line.

Exercise#python.m1.l3.e2
Attempts: 0Loading…

Given age = 20, assign category the string 'adult' if age >= 18 else 'minor' using the one-line ternary construct.

Loading editor…
Show hint

The ternary syntax puts the true result first, then if condition else false_result.

Solution available after 3 attempts

Final Challenge

Step 3 — Debugging a complete conditional chain

Test the three branches on a negative number while keeping the logical order clean.

Exercise#python.m1.l3.e3
Attempts: 0Loading…

Starting with x = -5, set sign to 'positive' (x > 0), 'negative' (x < 0), or 'zero' with if / elif / else, then evaluate sign.

Loading editor…
Show hint

Use if x > 0:, elif x < 0: and else:.

Solution available after 3 attempts