Programs that decide: the if statement, indentation as Python's grammar, elif chains and the else safety net.
01
Learning objectives
Write if / elif / else blocks with correct four-space indentation.
Explain that indentation — not brackets — defines which statements belong to a branch.
Trace a multi-way elif ladder and state that exactly one branch (or none) runs.
Predict the output of programs that combine comparisons with print.
Spot and fix the classic errors: missing colon, = inside a condition, wrong indent.
02
Lesson notes
Until now every program ran every line. Conditions change that: an if statement runs its block only when a question evaluates to True. The shape is if condition: — note the colon — followed by an indented block of statements. The condition is any expression that produces True or False (the comparison and logic operators from chapter 2), and the block is everything indented under the if. When the condition is False, Python skips the whole block and continues below it.
Indentation is not cosmetic in Python — it is the grammar. Other languages wrap blocks in { }; Python measures the spaces. The convention is four spaces per level, and every line of a block must use the same depth or Python answers with IndentationError. This is also why a missing colon is the beginner's classic: if marks >= 50 without the colon is a SyntaxError, flagged before the program even starts. Both mistakes are one-glance fixes once you can read the error's line number.
Two-way choices add else: — no condition, just a colon — whose block runs when the if's condition was False. Exactly one of the two blocks runs, never both, never neither. Multi-way choices stack elif (short for else-if) between them, and Python checks the ladder from the top: the first condition that is True runs its block and skips the rest of the ladder entirely. The else at the bottom is the safety net that catches every case no condition claimed. For grade bands, BMI categories or menu handling, the elif ladder IS the answer.
Trace any ladder with a two-column table: condition on the left, verdict True/False on the right, stopping at the first True. The subtle point that loses marks: conditions are checked in order, so an over-broad condition earlier in the ladder can shadow a later one. if percentage >= 40: placed above elif percentage >= 80: makes the A-grade branch unreachable — every percentage ≥ 80 is also ≥ 40 and gets caught first. Order ladders from most specific to most general.
Conditionals compose with everything you already know: the condition can call functions (if name.strip() == "":), use boolean operators (if marks >= 80 and attendance >= 75:), or chain comparisons (if 33 <= marks <= 100:). And blocks nest — an if inside an if — which the next lessons cover properly. What stays constant is the discipline: colon, four spaces, one question at a time, and a trace table whenever the branches get deep.
A condition selects one branch
03
Worked code examples
Pass or fail — the two-way form
The fed mark is 61, so the condition 61 >= 33 is True: the if-block's two lines run and the else-block is skipped. Feed 20 instead and the verdicts swap. Exactly one block ever runs.
marks = int(input())
if marks >= 33:
print("Pass")
print("Well done!")
else:
print("Fail")
print("Retry the paper")
Output
Pass
Well done!
The grade ladder — first True wins
The fed 74 fails the ≥ 80 check but passes ≥ 70, so grade becomes "A" and the remaining ladder is skipped. Ladders must be ordered from most specific to most general.
The conditional expression (ternary) chooses between two values inside one line — perfect for assignments, less readable for long logic. The statement form remains the workhorse.
n = 7
parity = "even" if n % 2 == 0 else "odd"
print(n, "is", parity)
Output
7 is odd
04
Key terminology
condition
An expression that evaluates to True or False and decides whether a branch runs.
block
The statements indented under if / elif / else that run together as one unit.
elif
else-if — the next question in a ladder, checked only when every earlier condition was False.
IndentationError
The error raised when a block's lines are not indented consistently.
ladder
A chain of if / elif / else tests checked top to bottom; the first True wins.
05
Common misconceptions
Common mistake
Every condition in an elif ladder is checked.
Correct understanding
Checking stops at the first True — its block runs and the rest of the ladder is skipped entirely.
Common mistake
Indentation only makes code look neat.
Correct understanding
In Python, indentation defines block membership. Wrong indent depth is not style — it is an IndentationError or a different program.
Common mistake
if marks = 88: is how you test equality.
Correct understanding
= assigns; == compares. Assignment inside a condition is a SyntaxError that protects you from this exact bug.
06
Check your understanding
1. n = 15. What does this print?
if n > 10:
print("A")
elif n > 5:
print("B")
else:
print("C")
2. What follows an if statement's condition and starts its block?
3. In an if/elif/else ladder, how many blocks can run?