Python if else for clear code decision paths
Python if else runs one block when a condition is true and another when it is false.
Add elif branches to test several conditions in order, where only the first match runs.
For a quick value choice, the ternary operator picks between two values on a single line.
Reach for a full if/else when a branch does real work, and the ternary when you only need one value.
Python If Else Example For Conditional Choices
Output:
Output will appear here...
Output:
Shipping: $7
How This Example Works
The condition compares the order total to a threshold, so only one branch prints.
Python evaluates the comparison to True or False, and the else branch becomes the default when the condition is false.
order_total >= 50evaluates toFalse, so theelseblock is selected.- The
ifblock is skipped because the condition is not true. - The output confirms the false branch ran and shows the fallback message.
elif: Test Multiple Conditions
elif (short for “else if”) adds more branches to a single decision.
Python checks each condition top to bottom and runs the block for the first one that is true, then skips the rest.
score = 82
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
else:
grade = "F"
print(grade) # B
Only one branch runs, so order the checks from highest priority to lowest.
Coming from another language, elif is Python’s version of else if.
One-Line if-else: The Ternary Operator
The ternary operator chooses between two values in a single expression.
It reads as value_if_true if condition else value_if_false.
order_total = 48
shipping = "Free" if order_total >= 50 else "$7"
print(shipping) # $7
This is the same decision as the first example, condensed to one line. Use it for a quick value assignment, not for branches that run statements or side effects. It also fits inside larger expressions, such as a list comprehension:
totals = [30, 60, 48]
labels = ["Free" if total >= 50 else "$7" for total in totals]
print(labels) # ['$7', 'Free', '$7']
Common Mistakes
Mistake 1: Using separate if statements instead of elif.
status = "vip"
# Wrong: both checks run independently
if status == "vip":
price = 0
if status == "member":
price = 5
status = "vip"
# Right: one branch runs in an if/elif/else chain
if status == "vip":
price = 0
elif status == "member":
price = 5
else:
price = 10
Two standalone if statements can both run, so later checks can overwrite earlier work.
Mistake 2: Comparing to None with ==.
token = None
# Wrong: equality can be customized by objects
if token == None:
print("No token")
token = None
# Right: None is a singleton; compare by identity
if token is None:
print("No token")
is avoids false matches from custom equality logic and reflects the intended identity check.
Mistake 3: Relying on truthiness when zero is valid.
discount = 0
# Wrong: 0 is valid but falsy
if discount:
print(f"{discount}% off")
discount = 0
# Right: check for None when 0 is meaningful
if discount is not None:
print(f"{discount}% off")
Truthiness treats 0 as False, so the branch never runs even when the value is legitimate.
When to Use Python if else
- Use
if/elsewhen a single true/false decision drives two distinct actions or messages. - Use
eliffor a few ordered conditions where only the first match should run. - Use the ternary operator to pick one value inline, as in
x = a if cond else b. - Avoid long
if/elifchains for many fixed cases; amatchstatement or a lookup dictionary is clearer. - Avoid the ternary when a branch needs several statements; a full
if/elsestays readable.
Related Features
match handles many fixed cases cleanly, and and/or combine conditions with short-circuit logic so only the needed checks run.
When conditions get long, parentheses make precedence clear and reduce mistakes.