The if elif else statement is used when a program needs to check multiple conditions. The if condition is checked first. If it is False, Python checks the elif conditions one by one. If none of the conditions are True, the else block is executed.
elif is short for else if. It allows Python to check another condition when the previous condition is False.
age = 15
if age >= 18:
print("Adult")
elif age >= 13:
print("Teenager")
else:
print("Child")
if condition1:
statement
elif condition2:
statement
else:
statement
The program checks the conditions from top to bottom.
number = 0
if number > 0:
print("Positive")
elif number < 0:
print("Negative")
else:
print("Zero")
You can use multiple elif statements to check several conditions.
marks = 75
if marks >= 90:
print("Grade A+")
elif marks >= 75:
print("Grade A")
elif marks >= 60:
print("Grade B")
else:
print("Grade C")
marks = 85
if marks >= 90:
print("Excellent")
elif marks >= 75:
print("Very Good")
elif marks >= 60:
print("Good")
elif marks >= 40:
print("Pass")
else:
print("Fail")
number = -10
if number > 0:
print("Positive number")
elif number < 0:
print("Negative number")
else:
print("Zero")
age = 25
if age < 13:
print("Child")
elif age < 20:
print("Teenager")
elif age < 60:
print("Adult")
else:
print("Senior Citizen")
day = "Monday"
if day == "Monday":
print("Start of the week")
elif day == "Friday":
print("Last working day")
elif day == "Sunday":
print("Holiday")
else:
print("Regular day")
temperature = 25
if temperature >= 35:
print("Very Hot")
elif temperature >= 25:
print("Warm")
elif temperature >= 15:
print("Cool")
else:
print("Cold")
marks = int(input("Enter your marks: "))
if marks >= 90:
print("Grade A+")
elif marks >= 75:
print("Grade A")
elif marks >= 60:
print("Grade B")
elif marks >= 40:
print("Grade C")
else:
print("Fail")
If the user enters 82:
Conditions can contain comparison and logical operators.
age = 25
has_id = True
if age >= 18 and has_id:
print("Entry allowed")
elif age >= 18:
print("ID required")
else:
print("Entry not allowed")
language = "Python"
if language == "Java":
print("Java selected")
elif language == "Python":
print("Python selected")
elif language == "PHP":
print("PHP selected")
else:
print("Unknown language")
choice = 2
if choice == 1:
print("Add Student")
elif choice == 2:
print("View Student")
elif choice == 3:
print("Delete Student")
else:
print("Invalid Choice")
a = 10
b = 5
operator = "+"
if operator == "+":
print(a + b)
elif operator == "-":
print(a - b)
elif operator == "*":
print(a * b)
elif operator == "/":
print(a / b)
else:
print("Invalid operator")
The else block is optional. You can use if and elif without an else.
marks = 80
if marks >= 90:
print("Excellent")
elif marks >= 75:
print("Very Good")
Python allows you to use many elif blocks when required.
number = 3
if number == 1:
print("One")
elif number == 2:
print("Two")
elif number == 3:
print("Three")
elif number == 4:
print("Four")
else:
print("Other number")
When multiple conditions are True, Python executes the first matching block and skips the remaining elif and else blocks.
number = 10
if number > 5:
print("Greater than 5")
elif number > 8:
print("Greater than 8")
else:
print("Other")
The order of conditions is important because Python checks them from top to bottom.
marks = 95
if marks >= 40:
print("Pass")
elif marks >= 90:
print("Excellent")
The second condition is never reached because the first condition already matches.
An if elif else structure can also be placed inside another condition.
age = 20
student = True
if age >= 18:
if student:
print("Adult student")
elif age >= 60:
print("Senior citizen")
else:
print("Adult")
else:
print("Minor")
name = input("Enter student name: ")
marks = int(input("Enter marks: "))
print("Student:", name)
if marks >= 90:
print("Grade: A+")
elif marks >= 75:
print("Grade: A")
elif marks >= 60:
print("Grade: B")
elif marks >= 40:
print("Grade: C")
else:
print("Grade: F")
If the user enters:
Enter student name: Rahul
Enter marks: 82
Question: Which keyword is used to check another condition after the previous if condition is False?