In Python, indentation is part of the logic. The lines indented under if, elif, else, for and while are the lines that belong to them, so moving a line changes what the program does.
This lesson continues Python transition and execution after mapping an algorithm into a Python function.
How does Python decide which lines are inside a block?
A line ending with a colon opens a block. Every line indented one level under it is inside. The first line indented back out is outside, and runs after the block finishes.
For branches, if, elif and else are checked in order, and only the first branch whose condition is True runs. else catches everything left over.
Worked example
Task: return “High” for marks of 70 or more, “Middle” for 40 to 69, and “Low” otherwise.
def band(mark):
if mark >= 70:
return "High"
elif mark >= 40:
return "Middle"
else:
return "Low"
Step 1, choose boundary tests. The edges are 70, 69, 40 and 39.
Step 2, trace each one.
| mark | mark >= 70 | mark >= 40 | result |
|---|---|---|---|
| 70 | True | not checked | High |
| 69 | False | True | Middle |
| 40 | False | True | Middle |
| 39 | False | False | Low |
Because the first branch catches 70 and above, the elif does not need to say “and below 70”.
The mistake to watch for
A common slip is to use separate if statements for choices that should exclude each other.
Mistaken answer:
def band(mark): result = "Low" if mark >= 70: result = "High" if mark >= 40: result = "Middle" return resultFor 80, the first
ifsets “High”, then the secondifis also True and overwrites it with “Middle”. The function returns “Middle” for a mark of 80.
The correction is to change the second if to elif, so it is only checked when the first was False. Testing a mark well above the highest boundary, such as 80, exposes this at once.
Indentation can fail in the same way inside loops:
total = 0
for n in [1, 2, 3]:
total = total + n
print(total)
This prints 1, 3, 6. If print(total) is moved back one level, it sits after the loop and prints only 6.
Check yourself
1. What is printed by check(15) and by check(3)?
def check(n):
if n > 10:
print("big")
print("done")
Show answer
check(15) prints big then done. check(3) prints only done, because print("done") is not indented under the if, so it always runs.
2. What does sign(0) return?
def sign(n):
if n > 0:
return "positive"
elif n < 0:
return "negative"
else:
return "zero"
Show answer
“zero”. 0 > 0 is False, 0 < 0 is False, so else runs.
3. Why is 40 a good test value for band?
Show answer
It sits exactly on the boundary of mark >= 40. If a student had written > instead of >=, 40 would return “Low” and expose the error.
Where this leads next
Next, learn to tell a crash from a quiet fault in distinguishing a syntax error from a wrong result. Run band with your own test values in the Python reasoning sandbox, or write the same branches in pseudocode and check them in the restricted pseudocode trace trainer.
Our online one-to-one Computer Science tuition can help if branch questions keep slipping at the boundaries.