Kodeclik Blog
Using Elif in Python to Handle Multiple Conditions Gracefully
Elif - what a strange name that is! What does it mean? It is actually short for “else if”. Every programming language has a construct like this but in Python it is called “elif”.
Elif is a form of conditional logic. Conditional logic is very important in coding because it allows you to make decisions in your code. We will walk you through what elif means, shows you practical examples, outlines its limitations, and introduces related constructs.
What does elif mean?
As mentioned, elif stands for "else if". It lets you check multiple expressions for True and execute a block of code as soon as one of the conditions is True.
The basic syntax looks like this:
if condition1:
# do something
elif condition2:
# do something else
elif condition3:
# another condition
else:
# fallback if none of the above
You can have as many elif clauses as needed, but only one block will be executed—the first one with a True condition.
Example 1: Grading System
Consider the following Python program:
score = 85
if score >= 90:
print("A")
elif score >= 80:
print("B")
elif score >= 70:
print("C")
else:
print("F")
Here, elif helps categorize a numeric score into letter grades, with conditions checked in order. Note that you use elif for all but the last clause.
It will print:
B
Example 2: Day of the Week
Here is another example:
day = "Wednesday"
if day == "Monday":
print("Start of the week")
elif day == "Wednesday":
print("Midweek checkpoint")
elif day == "Friday":
print("Almost weekend!")
else:
print("Just another day")
This example demonstrates using elif to react to different string values. It will print:
Midweek checkpoint
Example 3: Categorizing Temperature
Consider this code:
temp = 35
if temp > 40:
print("It's scorching!")
elif temp > 30:
print("It's hot")
elif temp > 20:
print("It's warm")
else:
print("It's cold")
This shows how elif handles numerical ranges in descending order of priority. The output will be:
It’s hot
Problems with elif
While elif is powerful, it does have limitations:
- Linear Evaluation: Each condition is checked one-by-one until one is True. This may be inefficient if earlier conditions are expensive to compute.
- Rigid Structure: If multiple conditions need to be checked independently (not mutually exclusive), elif won't help; you'll need separate if statements.
- Deep Nesting: Overuse of elif can lead to deeply nested or hard-to-read code, especially when combined with nested if blocks.
Alternatives to elif
If your conditions are not mutually exclusive, it is better to use multiple if statements like so:
if x > 0:
print("Positive")
if x % 2 == 0:
print("Even")
As another example consider:
age = 70
is_student = True
if age >= 65:
print("Senior discount applied.")
if is_student:
print("Student discount applied.")
This will output:
Senior discount applied.
Student discount applied.
In this case, both conditions can be true at the same time (a person can be a senior and a student), so using if...elif would prevent one of them from running. Multiple if statements ensure both discounts are applied independently.
Summary
The elif keyword in Python provides a structured way to handle multiple conditional branches. It's useful for decision trees like grading, classification, or categorizing input. However, it can become cumbersome for complex or non-exclusive conditions, and newer constructs may be better suited in such cases. Understanding when and how to use elif is key to writing clean and readable Python code.
Enjoy this blogpost? Want to learn Python with us? Sign up for 1:1 or small group classes.