When you are programming in Python, you often have to make decisions in your code about certain conditions. That is where conditional statements help. One keyword that is very important in Python’s conditional statements is elif. In this blog, we will discuss what is elif and how to use it.
Table of Contents
What is a Conditional Statement?
With the help of conditional statements in Python, we can run different pieces of code depending on whether certain conditions are true or false. If is the most basic conditional statement in Python, which checks the condition first and then runs the code if the condition is true.
Example:
x = 10
if x > 5:
print("x is greater than 5")
Output:
x is greater than 5
Here, x is 10 which is greater than 5, which makes the condition true. So, it will print “x is greater than 5” as shown in the above output.
But if you have more than one statement, then an elif statement will be used.
What Does elif Mean?”>
What Does elif Mean?
The keyword elif stands for “else if.” It’s used to check multiple conditions in a sequence. After an if statement, you can use elif to check additional conditions. Each elif statement only runs if the previous conditions are false.
For example, let’s say you want to check if a number is positive, negative, or zero. You can use elif to check for multiple conditions:
Example
x = 10
if x > 0:
print("x is positive")
elif x < 0:
print("x is negative")
else:
print("x is zero")
In this example:
- If x is greater than 0, it will print “x is positive.”
- If x is less than 0, it will print “x is negative.”
- If neither of those is true, it will print “x is zero.”
Master Python for the Real World!
Industry-Ready Training for Future-Ready Professionals. Learn from Experts, Build Projects, and Advance Your Career!
Why Use elif?
Instead of checking each condition with separate if statements, you can use elif to make your code easier to read and more organized. It helps you check different conditions in order without needing a lot of complicated code.
Example with elif
Let’s say you want to give grades based on a student’s score. Here’s how you might do it using elif:
Example:
score = 85
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
elif score >= 60:
print("Grade: D")
else:
print("Grade: F")
In this example:
- If the score is 90 or more, it will print “Grade: A.”
- If the score is 80 or more (but less than 90), it will print “Grade: B.”
- If the score is 70 or more (but less than 80), it will print “Grade: C.”
- If the score is 60 or more (but less than 70), it will print “Grade: D.”
- If none of the conditions are true, it will print “Grade: F.”
Master Python – Free Online Course!
Learn Python from scratch and gain skills to tackle real-world projects with Intellipaat Academy.
How Does It Work?
Python checks the conditions one by one:
- First, it checks if the score >= 90. If it’s true, it prints “Grade: A” and stops.
- If the first condition is false, it checks a score >= 80. If that’s true, it prints “Grade: B” and stops.
- If both of the above conditions are false, it checks the next condition, and so on.
Once it finds a true condition, Python stops checking the rest.
Get 100% Hike!
Master Most in Demand Skills Now!
Conclusion
So far in this article, we learned about conditional statements such as if and elif. It helps us to check the different cases one by one without writing a lot of unorganized code. With the help of elif, you can make your code easier to understand and more organized, even if your code contains multiple conditions. If you want to learn more about conditional statements and operators in Python, check out Intellipaat’s Python course.