Learning outcomes
By the end of this lesson you should be able to: choose between two courses of action with if and else; chain several choices with elif; combine conditions with and, or and not; and correctly indent the blocks that belong to each branch (which is how Python knows which statements to include).
Prerequisites
Lesson 3 (Input, output & operators). You need comparison operators — >, <, ==, and the others — which produce True or False, and to be comfortable that a single equals stores while a double equals asks.
The big idea
So far every lesson you wrote has been a straight run of instructions: the program did everything, in order, every time. That is a monotone recitation. This lesson gives the program a fork in the road. When the program reaches an if statement, it evaluates a condition — an expression that is True or False — and, depending on the answer, follows one path or the other. This is what separates a calculator from a programme that can, say, grade an exam or decide whether a patient's reading is normal.
Why this matters before the syntax
The first thing to internalise is that a condition is just a value — a boolean. The if statement does not do anything special to the condition; it simply looks at whether it is True or False. This is why you can use a variable holding a boolean (like has_id = True directly as a condition) as easily as a comparison it produces. A condition is not a special phrase; it is an expression you already know how to write, now used in a decision.
The second, and easily most confusing, thing is indentation. Python does not use braces to group statements the way many other languages do. It uses the indentation. Every statement indented further than the if line belongs to that branch, and the block ends when the indentation returns to its earlier level. This is not cosmetic. If you indent too little or too much, either the statements fall outside the branch (running regardless) or Python refuses to run at all with an IndentationError.
The third thing is the order of an elif chain. Python checks the conditions top to bottom and runs the first one that is True, then skips the rest. The order matters: if you test a broad condition before a narrow one, the narrow one may never be reached. Putting the most specific test first is usually the safer habit.
Worked example 1: the first fork
A single if statement with an else. Exactly one of the two blocks runs — never both, never neither.
x = 15
if x > 10:
print("big")
else:
print("small")bigRead the colon on the if and else lines carefully — it is required and easy to drop. The statements inside each branch are indented. Because 15 is greater than 10, the condition is True, so Python runs the indented line print("big") and skips the else block entirely. The else is not a thing that would run later; it is a fallback that runs only when the if condition is False.
Worked example 2: choosing from many with elif
When there are more than two possible outcomes, chain them. The first True condition wins and the rest are skipped.
score = 72
if score >= 85:
print("A")
elif score >= 70:
print("B")
else:
print("C")BWalk the chain. The first condition, score >= 85, is False, so Python ignores its block and moves on. The next, score >= 70, is True, so it runs print("B") and then jumps past the rest — the else does not run, because an elif already matched. The chains read from the most demanding test to the least; if you flipped the order to test >= 70 first, an A student would get B, because the looser condition would match before the stricter one got a chance.
Worked example 3: combining conditions
To require two things at once, use and. To accept either one, use or. To invert a condition, use not.
age = 20
has_id = True
if age >= 18 and has_id:
print("Welcome")
else:
print("Entry denied")WelcomeThe boolean operators and and or combine smaller booleans into a bigger one. With and, the result is True only if both sides are True — that is the right choice for a rule where every condition must be met, like being both old enough and carrying identification. The not operator drops in front of an expression to flip it, so not mood == "sad" is True whenever mood is anything other than sad. These are not new kinds of condition; they are ways to build richer ones from the comparisons you already have.
Worked example 4: three outcomes from a sign
A classic branch: decide whether a number is positive, negative, or zero. Three branches, so it uses if, elif, and else together.
n = -3
if n > 0:
print("positive")
elif n < 0:
print("negative")
else:
print("zero")negativeThere is a careful point hidden here. If you had written two separate if statements instead of an if/elif, both could run for the same value. With if/elif, exactly one branch runs — the first matching one — and the rest are skipped. That is the whole difference between a chain of decisions and a set of independent checks, and it is often the source of a subtle bug where a value answers two conditions at once.
Common mistakes
Watch outDropping the colon at the end of the if, elif or else line. if x > 10 is a syntax error; you need if x > 10:. The colon is how Python knows a block is about to begin.
Watch outMismatched indentation. All statements in a branch must line up at exactly the same indent. Mixing tabs and spaces, or indenting a statement by one level too far, breaks the block or raises IndentationError.
Watch outUsing if/if instead of if/elif for an exclusive choice. Two separate ifs both evaluate, so a value that matches both runs both blocks. An elif chain runs only the first match.
Watch outTesting a broad condition before a narrow one. In an elif chain the first True wins, so a loose test earlier will swallow cases intended for a later, stricter one. Order the tests from the most specific to the least.
Check your understanding
If score is 60, what does this chain print, and why? if score >= 85: print('A') elif score >= 70: print('B') else: print('C')
What does the condition age >= 18 and has_id require to be True?
Why does Python require consistent indentation inside an if block?
Multiple choice
Which line correctly starts a conditional block?
What does and produce when one side is True and the other is False?
In an if/elif/else chain, how many branches can run?
Which is the correct way to check whether a number is at least 18?
What happens if the two statements in an if block are indented by different amounts?
How do you invert a condition, so that it is True when the original is False?
A student scores 90. Why does the chain below print C instead of A? if score >= 70: print('C') elif score >= 85: print('A')
Which boolean operator would you use when a rule must satisfy ALL listed conditions?
Summary and next
A condition is just a boolean expression. if runs one block when it is True, else runs the fallback, and elif chains several mutually exclusive choices — running only the first match. Indentation groups the statements into each block, and and/or/not build richer conditions. Order your elif tests from most specific to least.
Next: loops. Lesson 4 gave your program the power to choose; the next lesson gives it the power to repeat — doing something many times, which is what computers are actually for, and the start of the word-count idea that runs through the whole PY4E course.