if, elif, else
Programs often need to choose what to do. A phone, for example, can show a warning when its battery is low and a different message when it has plenty of charge.
In this lesson, battery is the battery percentage: 0 means empty and
100 means full. You'll build a program that chooses a message based on
that number.
One condition: if
An if statement asks a question. Python runs the indented code below it
only when the answer is True.
Run this example:
battery = 15
if battery <= 20:
print("Charge your phone")
print("Battery check complete")
battery <= 20 asks, "Is the battery at 20% or less?" With battery = 15,
the answer is True, so the warning prints.
Notice the shape of the code:
- The
ifline contains a condition and ends with a colon (:). - The warning is indented by four spaces. It belongs to the
ifblock. - The last
printis back at the left edge. It runs whether the condition is true or false.
Try changing battery to 80 and running it again. Only
Battery check complete should print. An if without an else can skip
its block without choosing another action.
Your turn: show a low-battery warning
The phone has 10% charge. Add an if statement that prints Low battery
when battery is 20 or less. Put the print inside the if block.
Use battery in your condition so the program also works when its value changes.
battery = 10
# Write an if condition, then an indented print.
Expected output
Low battery
Two choices: if / else
What if we want a message for both low and healthy batteries? Add else
to say what happens when the if condition is false.
battery = 80
if battery <= 20:
print("Charge your phone")
else:
print("You have enough charge")
Here, 80 <= 20 is False, so Python skips the first message and prints
You have enough charge instead. Exactly one of these two messages prints.
else: lines up with if. It has a colon but no condition: it covers
all the cases that the if did not match. Each branch's print is indented.
Change battery to 20 and run the example again. It prints
Charge your phone because <= includes the boundary value, 20.
Your turn: decide whether to charge
Write a battery reminder using these two rules:
- At 20% or less, print
Plug in your charger. - Otherwise, print
No charger needed.
The phone below has 65% charge, so the second message should print.
Write one if/else statement below the assignment, with one print
in each branch. Keep battery = 65 for the goal shown here.
battery = 65
# Check whether battery is 20 or less.
# Print the matching message using if / else.
Expected output
No charger needed
After matching the goal, try battery = 10. Your code should now print
Plug in your charger. The displayed goal stays the same, so this experiment
will show a mismatch even when your code is correct. Restore 65 to match
the goal again.
Three choices: if / elif / else
Sometimes two messages aren't enough. Let's give a low, medium, or high
battery message. elif means "else if": it checks another condition only
if the earlier condition was false.
battery = 35
if battery <= 20:
print("Low battery")
elif battery <= 50:
print("Half full or less")
else:
print("Plenty of charge")
Python works down the branches in order:
- Is
35 <= 20? No, so skip the first branch. - Is
35 <= 50? Yes, so printHalf full or less. - Skip the remaining branch. A single
if/elif/elsechain runs only the first matching branch.
The middle branch covers values above 20 and up to 50. We don't need to
check "above 20" again: Python reaches the elif only after the first
condition is false.
Try 15, 50, and 80 in the example and predict each message before
running it.
Why the order matters
A battery of 15 satisfies both battery <= 20 and battery <= 50.
Checking <= 20 first gives it the low-battery warning. If we checked
<= 50 first, 15 would get the middle message and Python would never reach
the low-battery branch.
Also, keep elif as part of the same chain. Replacing it with another
if starts a separate decision, so both conditions could run and print
two messages.
Your turn: build a battery status display
Write one if/elif/else chain that follows these rules, in this order:
| Battery percentage | Message to print |
|---|---|
| 20 or less | Low battery |
| Above 20, up to and including 50 | Time to charge soon |
| Above 50 | Plenty of charge |
Use battery in each condition. Print exactly one message, with the spelling
shown in the table. The starting value is 35, so your elif branch should
produce the goal below.
battery = 35
# Use if for 20 or less.
# Use elif for 50 or less.
# Use else for everything remaining.
# Put one indented print in each branch.
Expected output
Time to charge soon
To check your other branches, try 20 (expect Low battery), 50 (expect
Time to charge soon), and 51 (expect Plenty of charge). The goal checker
still expects the message for 35; restore battery = 35 when you're done.
You can now use if for an optional action, if/else for two choices,
and if/elif/else for several choices.
See also: Comparisons and Logical Operators, Comparisons and Logical Operators.
Practise this in the free Python course, where every exercise checks your code instantly.