Imagine playing a game where your character always runs forward, even if they are about to fall off a cliff! Python uses decisions to make programs feel alive, choosing different paths depending on what happens.
Words to own
- if statement
- A block of code that only runs when a specific condition is True.
- Condition
- A question or test that the computer answers with either True or False.
- Comparison operators
- Symbols like == and > that let you compare two values to see how they relate.
- elif
- Short for 'else if', this lets you check another condition if the first one was False.
- else
- A final backup plan that runs if none of the previous conditions were True.
- Indentation
- The 4 spaces at the start of a line that tell Python which code belongs inside a decision block.
The Crossroads: What is an if statement?
A computer program normally runs straight down, line by line. But real life isn't a straight line. If it starts raining, you open an umbrella. If you are hungry, you eat a snack. We make decisions based on conditions.
In Python, we use an if statement to create a fork in the road. An if statement checks a condition. If that condition is True, Python runs the indented code directly underneath it. If the condition is False, Python completely ignores that code and skips past it.
- Step 1
1. Check the 'if' condition
Python evaluates the first expression. If it is True, it runs the indented code and exits the decision block.
- Step 2
2. Move to 'elif' (if needed)
If the first condition was False, Python moves down and evaluates the next 'elif' condition.
- Step 3
3. Evaluate extra 'elifs'
Python tests each 'elif' in order. As soon as it finds one that is True, it runs that block and exits.
- Step 4
4. Fall back to 'else'
If every single condition checked was False, Python runs the code inside the backup 'else' block.
The Secret Code of Comparison Operators
To write conditions, we use comparison operators. These are special mathematical symbols that compare two values and decide if the result is True or False.
Be very careful with the equals sign! A single equals sign assigns or stores a value in a variable. To ask 'is this equal to?', you must use a double equals sign. We also use exclamation-equals to check if two things are not equal.
score = 100
if score == 100:
print("Perfect score!")
if score != 50:
print("Your score is not fifty.")Adding Backup Plans with elif and else
Sometimes you have more than two choices. If you are ordering pizza, you might want a large size, but if that is sold out, you want a medium, and if all else fails, you will take a small.
In Python, we use elif to ask extra questions if the first if statement was False. We can have as many elif blocks as we want. At the very end, we can add a single else block as our ultimate catch-all backup. If absolutely none of the previous conditions were True, the else block runs.
temp = 15
if temp > 25:
print("It is hot!")
elif temp > 10:
print("It is warm.")
else:
print("It is freezing!")The Golden Rule of Indentation
How does Python know which lines belong inside your decision and which lines come after? It uses indentation! This means pushing the code lines inward by exactly 4 spaces.
Every if, elif, and else line must end with a colon. The lines of code that run when that condition is met must be indented. If you forget to indent, or use a different number of spaces, Python will get confused and show an IndentationError.
Your turn — run real Python
The Teenager Detector
We have a program that checks if someone is a teenager. Right now, it thinks a 12-year-old is just 'not a teenager yet'. Add an elif block directly after the if block so that if the age is exactly 12, it prints 'Almost a teenager!' instead.
Bug hunt
Why does this code cause a SyntaxError?
score = 50
if score = 100:
print("Perfect score!")Myth-busting corner
MythYou can run multiple blocks in an if-elif-else chain if more than one condition is True.
TruthOnly the very first True block will run. Once Python finds a condition that matches, it runs its code and immediately jumps straight to the end of the entire chain, ignoring any other conditions below it.
MythAn else block can take its own condition.
TruthAn else block cannot have a condition! It is a blind catch-all backup that simply runs when everything else above it fails.
Exam answer that scores full marks
Explain how an if/elif/else structure decides which line to run.
Python checks the conditions in order from top to bottom. It tests the 'if' condition first. If it is True, it runs that indented block of code and skips the rest of the structure. If it is False, it tests each 'elif' condition in order, running the first one that is True and skipping any remaining options. If none of the conditions are True, it runs the backup 'else' block. Crucially, only one block in the entire chain will ever run.
Why it scores
- Accurately describes the top-to-bottom checking process.
- Explains how True conditions cause their block to execute while skipping the remaining code.
- Correctly identifies the else block as the catch-all backup when all conditions are False.
- Emphasises that only one branch of code is executed.