Skip to content

Module 5 · Conditional Statements — topic 4 of 10

if-elif-else

Choose one of many actions with if-elif-else. Learn how Python tests conditions in order, why ordering matters for ranges, and when a dictionary lookup is clearer than a long chain.

Beginner 12 min read·Lesson 63 of 306 in Python Programming

Learning objectives

  • Write multi-way decisions with elif
  • Order range conditions correctly
  • Replace long equality chains with a dictionary

Definition

Choose one of many actions with if-elif-else. Learn how Python tests conditions in order, why ordering matters for ranges, and when a dictionary lookup is clearer than a long chain.

Introduction

When there are more than two possible outcomes, add elif (short for “else if”) branches between the if and the else. Python tests each condition from top to bottom and runs the first block whose condition is true. The final else, if present, handles everything that matched nothing.

Why it matters

Grades, tax slabs, shipping zones and menu choices all have several outcomes. if-elif-else expresses them as one structure in which at most one branch runs, which avoids the overwrite bugs of separate ifs.

Detailed explanation

Python checks conditions in order and stops at the first true one. Later conditions are not even evaluated.

Because of this, the order of range checks matters. Checking marks >= 40 before marks >= 75 would put a score of 90 in the lower band. Check the most restrictive condition first, or write each range with both bounds.

Thanks to the ordering, each elif can assume that all earlier conditions were false, which keeps conditions short: after if marks >= 75, a following elif marks >= 60 already knows marks is below 75.

else is optional. Without it, nothing happens when no condition matches, which is fine when “do nothing” is the right behaviour.

There is no limit on the number of elif branches, but a long chain comparing one variable to fixed values (if choice == "1"... elif choice == "2"...) is often clearer as a dictionary that maps each value to its result.

Syntax

Python
if condition_1:
action_1
elif condition_2:
action_2
elif condition_3:
action_3
else:
default_action
  • Conditions are tested top to bottom.
  • Only the first true branch runs.
  • else catches anything not matched; it is optional.

Code examples

Grades

Python
marks = 68
if marks >= 75:
grade = "Distinction"
elif marks >= 60:
grade = "First class"
elif marks >= 40:
grade = "Pass"
else:
grade = "Fail"
print(grade)
Output
First class

Order matters

Python
marks = 90
if marks >= 40:
print("Pass")
elif marks >= 75:
print("Distinction")
Output
Pass

The first true condition wins, so the distinction branch is never reached for any score.

More examples

Income-tax style slabs and a dictionary dispatch

Python
def tax(income):
if income <= 300_000:
return 0
elif income <= 700_000:
return (income - 300_000) * 0.05
elif income <= 1_000_000:
return 20_000 + (income - 700_000) * 0.10
else:
return 50_000 + (income - 1_000_000) * 0.15
for amount in [250_000, 500_000, 900_000, 1_200_000]:
print(amount, tax(amount))
shipping = {"local": 40, "state": 70, "national": 120}
zone = "state"
print("Shipping:", shipping.get(zone, "unknown zone"))
Output
250000 0
500000 10000.0
900000 40000.0
1200000 80000.0
Shipping: 70

Step-by-step explanation

  • Each slab is checked in increasing order of income, so each elif knows the lower bound already holds.
  • Income up to 3 lakh pays nothing; the next band pays 5% on the part above 3 lakh.
  • Higher bands add the fixed tax of the slabs below plus a percentage of the excess.
  • The dictionary replaces an if-elif chain that would compare zone with three strings.
  • get() returns a default message for a zone that isn’t listed.

Expected output

Running “Income-tax style slabs and a dictionary dispatch” prints exactly:

TextOutput
250000 0
500000 10000.0
900000 40000.0
1200000 80000.0
Shipping: 70

Practical applications

  • Tax calculators use if-elif chains for income slabs.
  • Command-line menus map the user’s choice to an action.
  • Delivery apps assign a time window based on distance bands.

Best practices

  • Order ranges from most to least restrictive.
  • Add an else to catch unexpected values when every input must be handled.
  • Use a dictionary for long equality chains.

Common mistakes

Wrong order of overlapping ranges

Avoid

if score >= 40: band = "pass"
elif score >= 75: band = "distinction"

Prefer

if score >= 75: band = "distinction"
elif score >= 40: band = "pass"

Put the narrower, higher range first; otherwise it can never be reached.

Using else if

Avoid

else if marks >= 40:

Prefer

elif marks >= 40:

Python’s keyword is elif; else if is a SyntaxError on one line.

Key takeaways

  • elif adds more branches; at most one branch runs.
  • Conditions are tested in order and the first true one wins.
  • else is optional and handles everything unmatched.

Try it yourself

  1. 1
    Classify a BMI value into underweight (< 18.5), normal (< 25), overweight (< 30) or obese.
  2. 2
    Replace an if-elif chain that maps "1", "2", "3" to "Add", "Edit", "Delete" with a dictionary.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

1 question test this exact topic. Answers and explanations are revealed after you submit.

  • BeginnerWhat is printed?
Practise this topic