Python 3 · Beginner
Python if, elif, and else: order, syntax, and useful patterns
Learn how Python evaluates an if / elif / else chain, avoid ordering mistakes, and know when guard clauses or match are clearer.
Python spells “else if” as the single keyword elif. A chain selects the first branch whose condition is true:
temperature = 24
if temperature >= 30:
label = "hot"
elif temperature >= 20:
label = "warm"
else:
label = "cool"
print(label) # warm
Python evaluates the comparison after if first. Because it is false, execution moves to elif. That condition is true, so Python assigns "warm" and skips the else suite.
The syntax that matters
Each condition ends with a colon. The statements controlled by it are indented:
if condition:
first_statement()
second_statement()
elif other_condition:
alternative()
else:
fallback()
Parentheses around the conditions are optional and usually omitted. Braces do not define blocks in Python; indentation does.
An if is required. You may add any number of elif clauses and zero or one else clause. If present, else must finish the chain.
Put narrow conditions before broad ones
Only the first true branch runs. A broad condition can hide a more specific case below it:
def shipping_cost(total):
if total >= 50:
return 5
elif total >= 100:
return 0
return 10
shipping_cost(120) # 5, not 0
The total >= 100 branch is unreachable. Every total that reaches 100 has already satisfied total >= 50.
def shipping_cost(total):
if total >= 100:
return 0
elif total >= 50:
return 5
return 10
Ordering thresholds from highest to lowest makes each category reachable.
elif versus another if
An elif belongs to one mutually exclusive decision. A separate if begins another decision.
roles = {"editor", "reviewer"}
if "editor" in roles:
print("Can edit")
if "reviewer" in roles:
print("Can review")
Both messages print. If the second statement were elif, Python would skip it after the editor condition matched.
Ask whether more than one block should run for the same input. If yes, use independent if statements.
Empty suites need pass
Python does not allow an empty indented block. During a sketch, use pass as a do-nothing placeholder:
if feature_enabled:
pass # implementation comes later
else:
use_stable_path()
An explanatory comment alone does not count as a statement; pass does.
Truth values in conditions
Python converts values to booleans when evaluating a condition. Empty collections, empty strings, numeric zero, None, and False are falsey. Most other values are truthy.
items = []
if items:
process(items)
else:
show_empty_state()
This is idiomatic because an empty list and “no items to process” mean the same thing. Compare explicitly when falsey values have different business meanings:
if discount is None:
discount = default_discount
Writing if not discount would also replace a deliberate zero discount.
Flatten validation with guard clauses
Deep nesting makes the successful path hard to follow:
def publish(article, user):
if user is not None:
if user.can_publish:
if article.is_valid:
return save(article)
return False
Early returns put exceptional cases first:
def publish(article, user):
if user is None:
return False
if not user.can_publish:
return False
if not article.is_valid:
return False
return save(article)
This uses several independent if statements because each one may end the function. The main operation stays unindented.
Conditional expression for one small value
Python’s conditional expression is useful when both branches produce one value:
access = "allowed" if user.is_active else "blocked"
Do not stretch it into complex nested expressions. A normal statement is easier to debug and can hold multiple lines.
When match is a better fit
Python 3.10 introduced structural pattern matching. Consider match when one value is being unpacked or compared against shapes:
match command:
case ["move", x, y]:
move_to(int(x), int(y))
case ["quit"]:
stop()
case _:
show_help()
An if / elif chain remains clearer for ranges, unrelated boolean expressions, and a short ordered decision.
The official language walkthrough is in the Python tutorial’s if statements section.
Examples were executed with Python 3.14.5. Behaviour and syntax were checked against the Python 3 tutorial's control-flow documentation.