Lesson 10 of 30 · Making Decisions
If, Elif and Else
Teach your Python programs to make decisions with if, elif and else. Understand indentation, the order of conditions and the mistakes to avoid.
- 6 min read
- Beginner
- Reviewed
- How to run code only when a condition is true
- How to choose between two or more paths with else and elif
- Why indentation matters in Python
Making a decision
Until now, your programs ran every line. An if statement lets a program choose: run some code only when a condition is True.
temperature = 35
if temperature > 30:
print("It is hot today.")
print("Drink some water.")
print("Have a nice day.")Change temperature to 20 and run it again. The two indented lines are skipped, but the last line still runs.
The shape of an if statement
Three details matter.
- The line starts with
if, followed by a condition that isTrueorFalse. - The line ends with a colon
:. - The lines that belong to the
ifare indented by four spaces. Together they are called a block.
Indentation is how Python knows which lines are inside the if. A line that returns to the left edge is outside it and always runs.
else: the other path
Add else to say what should happen when the condition is False. Exactly one of the two blocks runs, never both.
age = int(input("How old are you? "))
if age >= 18:
print("You can vote.")
else:
print("You are too young to vote.")Change the input to 18, then 30, and check the result each time.
elif: more than two paths
elif is short for "else if". It lets you test several conditions in order.
score = 82
if score >= 90:
print("Grade: A")
elif score >= 75:
print("Grade: B")
elif score >= 60:
print("Grade: C")
else:
print("Grade: F")Python checks the conditions from top to bottom and runs the first block whose condition is True. After that, it skips all the rest.
Order matters
Because only the first match runs, the order of your conditions changes the result. This version has a bug.
score = 95
if score >= 60:
print("Grade: C")
elif score >= 75:
print("Grade: B")
elif score >= 90:
print("Grade: A")A score of 95 gets a C, because 95 >= 60 is true and Python stops there. Put the strictest condition first, as in the earlier example.
if inside if
You can put one if inside another. Each level is indented by four more spaces.
has_ticket = True
age = 15
if has_ticket:
if age >= 18:
print("Enjoy the film.")
else:
print("You need an adult with you.")
else:
print("Please buy a ticket first.")Write an if, elif, else chain that prints the grade letter for score. Test it with other scores once it passes.
score = 82 # Print the grade for this score: # 90 or more -> A # 75 or more -> B # anything else -> C # For a score of 82 the output must be exactly: B
In an if / elif / else chain, how many blocks can run?
Summary
ifruns a block only when its condition isTrue.elseruns when the condition isFalse.elifadds more conditions, checked from top to bottom.- Only the first matching block runs, so order matters.
- Blocks are marked by a colon and four spaces of indentation.
Next: combining several conditions into one.
Finished reading? Tick it off to track your progress.