We use conditional statements in Python to control the execution flow of a program. In this article, we will discuss if vs elif vs else if in Python to get an understanding of how these conditional statements work.
If Statements in Python
If statements are used to execute certain Python statements when a particular condition is True. The syntax for if statements in Python is as follows.
#statements outside if block before the if statement
if condition:
#statements in if block
#statements outside if block after the if statement
Here, the condition
evaluates to a boolean value i.e. True or False. If the condition
is True, the statements in the if block are executed. Otherwise, the statements outside the if block are executed. You can observe this in the following code.
marks=50
if marks>40:
print("Pass")
Output:
Pass
Else Statement in Python
We use the else statement along with the if statement when we have to execute a task whenever the condition inside the if statement is False. It has the following syntax.
#statements outside if block before the if statement
if condition:
#statements in if block
else:
#statements inside the else block
#statements outside the if block after the if statement
Here, when the condition
in the if block is False, the statements in the else block are executed. You can observe this in the following example.
marks=34
if marks>40:
print("Pass")
else:
print("Fail")
Output:
Fail
Elif Statement in Python
We use elif statements in Python if we have to execute code based on multiple conditions. It has the following syntax.
if condition:
#statements in if block
elif condition1:
#statements in elif block 1
elif condition2:
#statements in elif block 2
.
.
.
elif condition N:
#statements in elif block N
#statements outside the if block after the if and elif statement
Here,
- If the condition inside the if block is True, the code in the if block is executed and the rest of the code in the elif blocks is skipped.
- When the
condition
inside the if block is False, the first elif block is executed. Ifcondition1
inside elif block 1 is True, the statements inside elif block 1 are executed and the rest of the code is skipped. - When the conditions inside if block and elif block 1 are False, the second elif block is executed. If the
condition2
inside the elif block 2 is True, the statements inside elif block 2 are executed and the rest of the code is skipped. - This process continues until one of the conditions in the if-elif blocks are true or all the conditions are false.
You can observe this in the following example.
marks=65
if marks>90:
print("A+")
elif marks >80:
print("A")
elif marks>70:
print("B+")
elif marks>60:
print("B")
elif marks>40:
print("Pass")
else:
print("Fail")
Output:
B
If all the conditions in the if and elif blocks are false, the codes written inside these blocks are not executed. You can observe this in the following code.
marks=23
if marks>90:
print("A+")
elif marks >80:
print("A")
elif marks>70:
print("B+")
elif marks>60:
print("B")
elif marks>40:
print("Pass")
print("Outside if elif statements.")
Output:
Outside if elif statements.
You can also add an else block with if and elif blocks in the code. If none of the conditions in the if and elif blocks are True, the statements in the else block are executed as shown below.
marks=23
if marks>90:
print("A+")
elif marks >80:
print("A")
elif marks>70:
print("B+")
elif marks>60:
print("B")
elif marks>40:
print("Pass")
else:
print("Fail")
print("Outside if elif statements.")
Output:
Fail
Outside if elif statements.
If Else If Statement in Python
We cannot use the else if statement in Python in a single statement. We can only use else-if statements in Python if we have to use nested conditional statements. For this, you can use the following syntax.
If condition:
#statements in if block
Else:
If condition 2:
#statements in the inner if block
Else:
#statememts in the inner else block
Here, if the condition in the outer if block is False, the code in the else block is executed. Again, if condition2
inside the inner if block is true, the code inside the inner if block is executed. Otherwise, the code inside the inner else block is executed. You can observe this in the following example.
marks=-34
if marks>40:
print("Pass")
else:
if marks>0:
print("Fail")
else:
print("Negative Marks")
Output:
Negative Marks
If vs Elif vs Else If in Python
After discussions in the previous sections, we can conclude the following remarks for If vs Elif vs Else if in Python.
- The If statement is used to execute a single conditional statement whereas elif statements are used with the if statement to execute multiple conditional statements.
- We use the if statement to execute code when a condition is True. On the other hand. We can use the else statement to execute code when the condition inside the if statement is False.
- We use elif statements in Python to execute multiple conditional statements whereas the else if blocks are used to execute nested conditional statements.
Conclusion
In this article, we discussed how to execute If vs Elif vs Else if statements in Python. To read more about Python programming, you can read this article on working with toml files in Python. You might also like this article on Python finally keyword.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Happy Learning!
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.