Making decisions: if, elif and else
Let a program choose what to do. Write if statements, add an else for the other case, chain choices with elif, and see why indentation matters in Python.

In this lesson you will
- Write an if statement with a condition, a colon and an indented block
- Add else for the case where the condition is False
- Chain several choices with elif and explain why only the first true branch runs
- Explain why indentation matters in Python, and fix an IndentationError
In the last lesson you learned to ask yes-or-no questions. Now you’ll make the program act on the answers. This is the moment a list of instructions becomes something that feels a little bit smart.
if: do this only when…
An if statement runs some code only if a condition is True:
temperature = 34
if temperature > 30:
print("It's hot! Drink some water.")
print("Have a nice day.")Three parts to notice:
- The word
if, then a condition (any expression that givesTrueorFalse). - A colon
:at the end of the line. It means “here comes the block”. - The block: one or more lines indented by four spaces underneath. These lines belong
to the
ifand run only when the condition isTrue.
The last line isn’t indented, so it isn’t part of the if. It runs either way. Change
temperature to 20 and run again: the first message disappears and the last one stays.
Indentation is part of the code
In many languages indentation is just for neatness. In Python it’s how you say which lines belong together. Every line in a block must be indented by the same amount; four spaces is the standard, and the code cells insert them for you when you press Tab.
money = 12
price = 8
if money >= price:
print("You can buy it.")
money = money - price
print("Money left:", money)
print("Thanks for visiting.")All three indented lines run together, or not at all. Change money to 5 to see the other case.
If the indentation is inconsistent, Python refuses to run the cell and says so:
IndentationError: unexpected indent
or, if you forget to indent after the colon, IndentationError: expected an indented block.
Line the block up neatly and the error goes away.
else: otherwise…
Often you want to do one thing if the condition is True and something else if it’s
False. That’s what else is for. It has no condition of its own, just a colon:
number = 7
if number % 2 == 0:
print(number, "is even")
else:
print(number, "is odd")Exactly one of the two blocks runs, never both, never neither. Remember the % remainder
trick from the arithmetic lesson? Here it’s doing real work.
elif: more than two choices
What about three or more cases? elif (short for “else if”) adds another condition to check.
Python checks the conditions from top to bottom and runs the block of the first one that
is True, then skips the rest of the chain. If none are true, the else block runs.
The flowchart below shows that path. Move the temperature slider and press Run to send the token through the chart. Each diamond is a question; the token follows the True or False arrow.
Here is the if statement as a flowchart. Each diamond asks a question, and the token follows the answer.
Set the temperature to 35. The first question, temp > 30, is True, so Hot! is printed and
the chain ends. The second question is never asked, even though 35 is also greater than 15.
That’s the key rule: only the first true branch runs. Try the same in code:
temp = 35
if temp > 30:
print("Hot!")
elif temp > 15:
print("Mild")
else:
print("Cold!")
print("Done")Order matters
Because the first true branch wins, the order of your conditions matters. Here’s a grading program with a bug. Predict what a score of 95 gets, then run it:
score = 95
if score >= 50:
grade = "C"
elif score >= 70:
grade = "B"
elif score >= 90:
grade = "A"
else:
grade = "F"
print(grade)It prints C! A score of 95 is at least 50, so the first branch wins and the others are never
checked. The fix is to ask the strictest question first. Rearrange the code so 95 gets an
A, 75 a B, 55 a C, and 30 an F, and test each score.
Putting decisions inside decisions
A block can contain any code, including another if. This is called nesting:
has_ticket = True
age = 10
if has_ticket:
if age < 12:
print("Welcome! Children's seats are on the left.")
else:
print("Welcome! Find your seat.")
else:
print("Sorry, you need a ticket.")Each level of nesting adds four more spaces. Nesting is powerful but quickly hard to read; often
and can flatten it. Two or three levels is plenty.
Your turn
Exercise · Python
Letter grades
Solved ✓Turn a test score (0 to 100) into a letter grade with if/elif/else:
| Score | Grade |
|---|---|
| 90 or more | "A" |
| 75 to 89 | "B" |
| 50 to 74 | "C" |
| below 50 | "F" |
The starter has its conditions in the wrong order. Fix it so every score gets the right grade. Once it works for 82, test it yourself with 95, 75, 74, 50 and 30: the scores right on a boundary are the ones most likely to catch a bug.
score = 82
if score >= 50:
grade = "C"
elif score >= 75:
grade = "B"
elif score >= 90:
grade = "A"
else:
grade = "F"
print(score, "->", grade)
Check yourself
Self-check · 4 questions