Show contents
Chapter 1 of 6 · 15 min
if, elif, else: choosing a path
By the end of this chapter, you'll be able to
- Write a condition with if
- Add a default case with else
- Chain several cases with elif
- Indent a block of code correctly
Choosing with if
Think of a road sign: "if you're going to the airport, turn right". In Python, this decision is written with if. A condition is an expression whose value is True or False: these two values are called booleans.
temperature = 31
if temperature > 30:
print("Remember to drink water!")
print("Have a nice day")
Remember to drink water!
Have a nice day
Three details matter:
- the
ifline ends with a colon:; - the lines that depend on the
ifare indented, meaning shifted right by 4 spaces: they form a block; - the last line isn't indented: it runs in every case.
A default case with else, more cases with elif
else runs when the condition is false. elif (short for "else if") adds an intermediate case. Python tests the conditions from top to bottom and only runs the first block whose condition is true.
Line 8: after the if, the program carries on as normal and displays the advice.
Text description of the animation
- The program picks some advice based on the temperature. Only one of the three branches will run.
- Line 1: temperature is 18.
- Line 2: the first condition is false, we skip its block.
- Line 4: the elif condition is true.
- Line 5: we run this block. The else won't even be looked at.
- Line 8: after the if, the program carries on as normal and displays the advice.
temperature = 18
if temperature > 30:
advice = "Drink some water"
elif temperature > 15:
advice = "It's mild"
else:
advice = "Take a jacket"
print(advice)
It's mild
You can write as many elifs as you need. The else is optional, but it must always come last.
Your turn
Mini-lab: the gradePython 3.13
Output
Click “Run”: Python will load in your browser (about 10 MB the first time).Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Worked exercise
Worked exercise: the bus fare
A bus ticket costs 0 for children under 6, 1 for ages 6 to 17, and 2 from age 18. Write a program that starts from a variable age and displays the price.
Hint 1
if, elif and else.Hint 2
age < 6, then age < 18.Show the solution and common mistakes
age = 12
if age < 6:
price = 0
elif age < 18:
price = 1
else:
price = 2
print("Price:", price)
Price: 1
Common mistakes:
- Writing three separate
ifs: forage = 3, the first two would both be true and the price would be overwritten. - Forgetting the colon at the end of the
if: Python raises aSyntaxError.
Summary
- 1if runs a block only if its condition is True.
- 2elif adds a case, else handles all the other cases.
- 3Python tests the conditions from top to bottom and only runs the first true block.
- 4A block is indented by 4 spaces, after a line ending with a colon.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.