Statement

if statement

An if statement runs a block of code only when a condition is true, with elif and else for the other cases.

BeginnerEvery example below runs in your browser. Change the code and press Run.

if condition: # runs when condition is true elif other_condition: # optional, any number of these else: # optional: runs when nothing above was true

if lets your program make a choice. Python checks the condition after if; when it is true, the indented block below it runs. When it is false, the block is skipped.

Add elif (short for "else if") to test more conditions in order, and else to catch everything that is left. Python stops at the first branch whose condition is true, so at most one branch runs.

The condition does not have to be True or False. Empty things count as false: 0, "", [], {} and None. Everything else counts as true, so if items: is the usual way to ask "is this list not empty?".

Examples

1A simple yes or no

Pythoneditable
age = 20
if age >= 18:
    print("You can vote")
else:
    print("Too young to vote")
Output
You can vote

2Several cases with elif

Python tests the conditions from top to bottom and takes the first one that is true.

Pythoneditable
score = 74
if score >= 90:
    grade = "A"
elif score >= 75:
    grade = "B"
elif score >= 60:
    grade = "C"
else:
    grade = "F"
print("Grade:", grade)
Output
Grade: C

74 fails >= 90 and >= 75, then passes >= 60, so the later branches are never checked.

3Check if a list is empty

Pythoneditable
basket = []
if basket:
    print("Items:", basket)
else:
    print("Your basket is empty")
basket.append("bread")
if basket:
    print("Items:", basket)
Output
Your basket is empty
Items: ['bread']

An empty list counts as false and a list with items counts as true. No len() needed.

4Combine conditions with and / or

Pythoneditable
temperature = 24
raining = False
if temperature > 20 and not raining:
    print("Picnic in the park")
elif raining or temperature < 5:
    print("Stay inside")
else:
    print("Take a jacket")
Output
Picnic in the park

5One-line choice: the conditional expression

When you only need to pick between two values, a if condition else b fits on one line.

Pythoneditable
stock = 0
label = "in stock" if stock > 0 else "sold out"
print("Blue mug:", label)
Output
Blue mug: sold out

Common mistakes

Using = instead of ==

A single = stores a value; == compares two values. if x = 5: is a SyntaxError. Write if x == 5:.

Writing several ifs when you mean elif

Separate if statements are all checked, so more than one block can run. Use elif when only one branch should win.

Pythoneditable
points = 95
if points > 50:
    print("Silver")
if points > 90:
    print("Gold")
print("---")
if points > 90:
    print("Gold")
elif points > 50:
    print("Silver")
Output
Silver
Gold
---
Gold

Comparing against several values the wrong way

if color == "red" or "blue": is always true, because "blue" on its own is a non-empty string. Use in with a tuple instead.

Pythoneditable
color = "green"
if color == "red" or "blue":
    print("Wrong test says: warm or cool")
if color in ("red", "blue"):
    print("Right test says: red or blue")
else:
    print("Right test says: something else")
Output
Wrong test says: warm or cool
Right test says: something else

Questions people ask

Does Python have a switch statement?

Since Python 3.10 there is match / case, which compares one value against many patterns. For a few simple cases, an if / elif chain or a dictionary lookup is just as clear.

How do I check if a value is between two numbers?

Chain the comparisons: if 10 <= x < 20:. Python reads it as 10 <= x and x < 20.

How many elif branches can I have?

As many as you like. else is optional and, if present, must come last.

What does if not mean?

not flips true and false. if not items: runs when the list is empty. See and, or, not (logical operators).

Spotted a mistake on this page? Tell us