Introduction
Python provides the elif statement. The elif statement allows you to check multiple conditions sequentially. When one condition evaluates to True, the corresponding block of code executes, and the remaining conditions are skipped.
In this tutorial, you will learn what the Python elif statement is, its syntax, examples, real-life applications, common mistakes, interview questions, and best practices.
What is Python Elif Statement?
The elif statement is used to check multiple conditions after an if statement. It provides additional conditions that Python evaluates only if the previous conditions are false.
The word elif is a combination of:
- else
- if
Example:
marks = 75
if marks >= 90:
print("Grade A")
elif marks >= 70:
print("Grade B")
else:
print("Grade C")
Output:
In this example:
- The first condition is false.
- Python checks the elif condition.
- Since it is true, “Grade B” is displayed.
- The remaining conditions are skipped.
Why Use Elif Statements?
Elif statements are useful when:
- Multiple conditions need to be checked.
- Different outcomes are possible.
- Programs require more than two choices.
- Complex decision-making logic is needed.
Without elif, developers would need to use multiple nested if statements, making code harder to read.
Syntax
The basic syntax of an elif statement is:
if condition1:
# code block
elif condition2:
# code block
elif condition3:
# code block
else:
# code block
Syntax Breakdown
- if checks the first condition.
- elif checks additional conditions.
- else executes if no conditions are true.
- Only one block executes.
How Elif Works
Python evaluates conditions from top to bottom.
- Check the if condition.
- If false, check the first elif.
- If false, check the next elif.
- Continue until a condition becomes true.
- Execute the matching block.
- Skip the remaining conditions.
- If no condition matches, execute the else block.
Simple Elif Example
number = 0
if number > 0:
print("Positive")
elif number < 0:
print("Negative")
else:
print("Zero")
Output:
This example classifies a number as positive, negative, or zero.
Example: Student Grade System
marks = 85
if marks >= 90:
print("Grade A")
elif marks >= 80:
print("Grade B")
elif marks >= 70:
print("Grade C")
elif marks >= 60:
print("Grade D")
else:
print("Fail")
Output:
This is a common real-world use of the elif statement.
Example: Day of the Week
day = 3
if day == 1:
print("Monday")
elif day == 2:
print("Tuesday")
elif day == 3:
print("Wednesday")
else:
print("Invalid Day")
Output:
Example: Traffic Signal System
signal = "Yellow"
if signal == "Green":
print("Go")
elif signal == "Yellow":
print("Slow Down")
elif signal == "Red":
print("Stop")
else:
print("Invalid Signal")
Output:
Traffic control systems use similar logic.
Using Comparison Operators with Elif
Comparison operators are commonly used in elif conditions.
| Operator | Description |
|---|---|
| == | Equal to |
| != | Not equal to |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
Example:
age = 65
if age < 13:
print("Child")
elif age < 20:
print("Teenager")
elif age < 60:
print("Adult")
else:
print("Senior Citizen")
Output:
Using Logical Operators with Elif
Logical operators help create more complex conditions.
| Operator | Description |
|---|---|
| and | Both conditions must be true |
| or | At least one condition must be true |
| not | Reverses the condition |
Example:
age = 25
if age = 18 and age <= 60:
print("Adult")
else:
print("Senior")
Output:
Multiple Elif Statements
Python allows multiple elif statements in the same block.
score = 95
if score >= 90:
print("Excellent")
elif score >= 80:
print("Very Good")
elif score >= 70:
print("Good")
elif score >= 60:
print("Average")
else:
print("Needs Improvement")
Output:
Elif with User Input
marks = int(input("Enter marks: "))
if marks >= 90:
print("Grade A")
elif marks >= 75:
print("Grade B")
elif marks >= 50:
print("Grade C")
else:
print("Fail")
Output:
Grade B
Nested Elif Statement
You can combine nested if statements with elif.
age = 25
if age >= 18:
if age >= 21:
print("Adult Member")
else:
print("Basic Member")
elif age >= 13:
print("Teen Member")
else:
print("Child Member")
Output:
Real-Life Examples:
1. Online Shopping Discount
purchase_amount = 7000
if purchase_amount >= 10000:
print("20% Discount")
elif purchase_amount >= 5000:
print("10% Discount")
elif purchase_amount >= 2000:
print("5% Discount")
else:
print("No Discount")
Output:
E-commerce websites often use similar logic.
2. Employee Bonus System
experience = 6
if experience >= 10:
print("Bonus: ₹50,000")
elif experience >= 5:
print("Bonus: ₹20,000")
elif experience >= 2:
print("Bonus: ₹10,000")
else:
print("No Bonus")
Output:
Companies commonly use this type of decision-making.
3. Weather Application
temperature = 32
if temperature >= 40:
print("Very Hot")
elif temperature >= 30:
print("Hot")
elif temperature >= 20:
print("Pleasant")
else:
print("Cold")
Output:
Weather apps frequently use multiple conditions like these.
Advantages of Elif Statements
1. Simplifies Multiple Conditions
Makes handling many conditions easier.
2. Improves Readability
Code becomes cleaner and more organized.
3. Reduces Nested Conditions
Avoids deeply nested if statements.
4. Efficient Execution
Python stops checking conditions once a match is found.
5. Ideal for Real-World Applications
Useful in grading systems, login systems, discount calculations, and more.
Common Mistakes
1. Using Else with a Condition
Incorrect:
if age > 18:
print("Adult")
else age < 18:
print("Minor")
Output:
Correct:
if age > 18:
print("Adult")
elif age < 18:
print("Minor")
2. Missing Colon
Incorrect:
if marks >= 90
print("A")
Output:
Correct:
if marks >= 90:
print("A")
3. Incorrect Indentation
Incorrect:
if marks >= 90:
print("A")
Output:
Correct:
if marks >= 90:
print("A")
4. Wrong Order of Conditions
Incorrect:
marks = 95
if marks >= 50:
print("Pass")
elif marks >= 90:
print("A Grade")
Output:
The first condition is already true.
Correct:
if marks >= 90:
print("A Grade")
elif marks >= 50:
print("Pass")
5. Using = Instead of ==
Incorrect:
if age = 18:
print("Adult")
Output:
Correct:
if age == 18:
print("Adult")
Conclusion
The Python elif statement is a powerful decision-making tool that allows programs to evaluate multiple conditions efficiently. It serves as an extension of the if statement and eliminates the need for deeply nested conditional structures. By checking conditions sequentially and executing only the first matching block, elif helps create cleaner, more readable, and more efficient code.