elif, and, or, not · Python from Zero
What if there are more than two choices, or several conditions at once?
Course contentsWhy you need it
Grades aren't just pass/fail: they're A, B, C or F. Free shipping might need "order over 50 and inside the country". elif handles many choices; and, or, not combine conditions.
The intuition
elif means "else, if...". Python checks the conditions from top to bottom and runs the first one that is True, then skips the rest:
if score >= 80:
print("A")
elif score >= 50:
print("C")
else:
print("F")A score of 90 is also >= 50, but only "A" prints: the first match wins. So put the strictest test first.
Combining conditions:
a and b: True only if both are True.a or b: True if at least one is True.not a: flips True and False.
Definition
elif
elif (else if) adds another condition to an if statement; the first condition that is True has its block run and the rest are skipped.
Logical operator
The logical operators and, or and not combine or invert Boolean values.
Examples
Letter grades
Try it yourselfTry 95, 70, 55 and 20.
score = int(input())
if score >= 80:
print("A")
elif score >= 65:
print("B")
elif score >= 50:
print("C")
else:
print("F")70
- L270 >= 80? False: skip.
- L470 >= 65? True: this block runs.
B - L6The remaining checks are skipped entirely, even though 70 >= 50.
and / or / not
Try it yourselfChange temp and raining and predict the output before running.
temp = 28
raining = False
print(temp > 25 and not raining)
print(temp < 10 or raining)- L3temp > 25 is True; not raining is True; True and True → True.
True - L4temp < 10 is False; raining is False; False or False → False.
False
Common mistakes
Wrong order of elif
If score >= 50 comes before score >= 80, an 85 gets the lower grade. Put the strictest condition first.
`x == 1 or 2`
This doesn't mean "x is 1 or 2". Write both comparisons in full: x == 1 or x == 2.
Quick quiz
1. What does this print?
x = 15
if x > 10:
print("A")
elif x > 5:
print("B")
else:
print("C")Show the answer
Answer: A
The first True condition wins; the elif is never checked.
2. What does this print?
print(5 > 3 and 2 > 4, 5 > 3 or 2 > 4)Show the answer
Answer: A
and needs both (one is False → False); or needs one (True).
Practice
3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.
- PredictWeather words
Type exactly what this program prints.
- ModifyAdd grade B
Add a grade B for scores from 65 to 79. The others stay: A for 80+, C for 50–64, F below 50.
- WriteLeap year
Read a year. Print
leapif it is a leap year, otherwisecommon. A year is a leap year if it is divisible by 4 and not by 100, or divisible by 400.
Practise this lesson
Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.
Interactive lessons open in order. If you have not done the earlier ones yet, you start at the course map.
Terms in this lesson
| Term | Chinese | Definition |
|---|---|---|
| elif | elif 分支 | An extra condition in an if statement, checked only if the ones above were False. |
| Logical operator | 逻辑运算符 | and, or and not, which combine or invert Boolean values. |
| First match wins | 首个匹配生效 | In an if/elif chain only the first True condition's block runs. |