Elif Statement
3 min read ·
The
elif statement stands for “else if”.
It is used when you want to check multiple conditions one by one.Python checks conditions from top to bottom:
- First
if - Then
elif - Finally
else(if present)
As soon as one condition becomes
True, the remaining conditions are skipped.Syntax of elif
elifmust come after if- You can use multiple elif
- Colon
:and indentation are mandatory
Note
You cannot use elif without an if.
Basic Example of elif
- If first condition fails, Python checks
elif - If
elifis true, its block executes
if – elif – else Flow
- Only one block executes
- Python stops checking after first match
Multiple elif Conditions
elif with Comparison Operators
elif with Logical Operators
elif with User Input
elif vs Multiple if
Using Multiple if (Not Recommended)
Both conditions execute.
Using elif (Recommended)
Only one condition executes.
Pro Tip
Use elif when conditions are mutually exclusive.
Nested elif
Real World Example – Electricity Bill
Real World Scenario
Electricity bill calculation based on units consumed
Common Errors with elif
elif Without if ❌
Wrong Order ❌
Higher condition should come first.
Stop
Wrong order can give logical errors even if syntax is correct.
Exercise
Practice Task
- Create a program to check grades using elif
- Check if a number is single-digit, double-digit, or more
- Build a simple traffic signal system using elif