Lesson 19 of 70 – Python If Elif Else
27%

Python If Elif Else

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.

Note: You can use multiple elif blocks between an if and an else statement.
What is elif?

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")
Output:
Teenager
Syntax of if elif else
if condition1:
    statement
elif condition2:
    statement
else:
    statement

The program checks the conditions from top to bottom.

Simple elif Example
number = 0

if number > 0:
    print("Positive")
elif number < 0:
    print("Negative")
else:
    print("Zero")
Output:
Zero
Multiple elif Conditions

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")
Output:
Grade A
Grade Program
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")
Output:
Very Good
Checking Positive, Negative and Zero
number = -10

if number > 0:
    print("Positive number")
elif number < 0:
    print("Negative number")
else:
    print("Zero")
Output:
Negative number
Checking Age Group
age = 25

if age < 13:
    print("Child")
elif age < 20:
    print("Teenager")
elif age < 60:
    print("Adult")
else:
    print("Senior Citizen")
Output:
Adult
Day Checking Program
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")
Output:
Start of the week
Checking Temperature
temperature = 25

if temperature >= 35:
    print("Very Hot")
elif temperature >= 25:
    print("Warm")
elif temperature >= 15:
    print("Cool")
else:
    print("Cold")
Output:
Warm
Taking Input with elif
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:

Output:
Grade A
Multiple Conditions

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")
Output:
Entry allowed
elif with Strings
language = "Python"

if language == "Java":
    print("Java selected")
elif language == "Python":
    print("Python selected")
elif language == "PHP":
    print("PHP selected")
else:
    print("Unknown language")
Output:
Python selected
Menu Selection Example
choice = 2

if choice == 1:
    print("Add Student")
elif choice == 2:
    print("View Student")
elif choice == 3:
    print("Delete Student")
else:
    print("Invalid Choice")
Output:
View Student
Simple Calculator with elif
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")
Output:
15
elif without else

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")
Output:
Very Good
Multiple elif Blocks

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")
Output:
Three
Only the First Matching Block Executes

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")
Output:
Greater than 5
Important: The second condition is also True, but it is not checked for execution because the first condition already matched.
Order of Conditions Matters

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")
Output:
Pass

The second condition is never reached because the first condition already matches.

Nested if with elif

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")
Output:
Adult student
Practical Student Grade Program
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
Output:
Student: Rahul
Grade: A
Key Points
  • elif means "else if".
  • It is used to check multiple conditions.
  • Python checks conditions from top to bottom.
  • The first matching condition is executed.
  • The else block is optional.
  • You can use multiple elif blocks.
  • Conditions can contain comparison and logical operators.
  • The order of conditions is important.
  • Indentation is required inside each block.
  • if elif else is commonly used for grading, menus, calculators, categories, and decision-making.

🧠 Quick Quiz

Question: Which keyword is used to check another condition after the previous if condition is False?