if Statements
You’ll learn how to run a block of code only when a condition is true. You do that with if, and optionally else and elif. The condition is an expression that’s either true or false (e.g. age >= 13). You’ll see more ways to write conditions on the Comparison Operators and Logical Operators pages.
Running code when a condition is true
Sometimes you want the program to do one set of steps in one situation and a different set in another. For example: in a movie app, recommend family-friendly movies when the user is under 13, and a wider range when they’re 13 or older.
In Python you use the keyword if, then a condition, then a colon. The code that runs when the condition is true is indented underneath.
age = 15
if age < 13:
# recommend family-friendly movies
if age >= 13:
# recommend a wider range of moviesThe program checks the value of the age variable. If age < 13 is true, it runs the first block. If age >= 13 is true, it runs the second block. Here age is 15, so the first block is skipped and the second runs.
We can make this clearer and avoid checking the same idea twice by using else.
else: when the condition is false
else means “do this when the if condition was false.” You don’t write a new condition after else—you’re just saying “otherwise, run this block.”
In the movie example, if the user is not under 13, they must be 13 or older. So we only need to check once:
age = 15
if age < 13:
# recommend family-friendly movies
else:
# recommend a wider range of moviesIf age < 13 is true, we run the first block. Otherwise we run the else block. One check, two outcomes.
elif: more than two cases
When you have three or more cases, use elif (short for “else if”). Python checks the if first, then each elif in order, then else if nothing was true. As soon as one condition is true, it runs that block and skips the rest.
Example: different recommendations for under 13, 13–17, and 18+:
age = 15
if age < 13:
# recommend family-friendly movies
elif age < 18:
# recommend teen movies
else:
# recommend adult moviesPython checks age < 13 first. If that’s false, the person is 13 or older, so elif age < 18 correctly catches 13–17. Everyone else (18+) hits the else. The order of the conditions does the work.
Remember: elif is only checked when the previous if (and any earlier elif) was false. So you’re choosing exactly one branch.
if vs elif: when to use which
An if always runs its check. An elif only runs its check when all the conditions above it were false. So you can’t always swap them.
Example: we want to congratulate a student for perfect attendance and for being on the honor roll. If we use elif for the second message, only one message prints:
student_has_perfect_attendance = True
student_on_honor_roll = True
if student_has_perfect_attendance:
print("Congrats on perfect attendance!")
elif student_on_honor_roll:
print("Congrats on making the honor roll!")
# Output: Congrats on perfect attendance!
# (Honor roll message is skipped—elif only runs when the first condition was false.)Because the first condition is true, Python runs that block and skips the elif. To print both messages when both are true, use two separate if statements:
student_has_perfect_attendance = True
student_on_honor_roll = True
if student_has_perfect_attendance:
print("Congrats on perfect attendance!")
if student_on_honor_roll:
print("Congrats on making the honor roll!")
# Output:
# Congrats on perfect attendance!
# Congrats on making the honor roll!Each if is checked on its own, so both blocks can run.
Summary
- Use
ifto run code only when a condition is true. - Use
elsefor “otherwise” andelifwhen you have several cases and want to pick one. - The condition is an expression that’s true or false. On the Comparison Operators and Logical Operators pages you’ll see how to write and combine conditions. You’ll use the same ideas in loops, which run a block of code multiple times.