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.
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
- Conditions are tested top to bottom.
- Only the first true branch runs.
- else catches anything not matched; it is optional.
Code examples
Grades
First class
Order matters
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
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:
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
Prefer
Put the narrower, higher range first; otherwise it can never be reached.
Using else if
Avoid
Prefer
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
- 1Classify a BMI value into underweight (< 18.5), normal (< 25), overweight (< 30) or obese.
- 2Replace 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?