Elif Condition
-
The elif condition in Python is used to check multiple conditions after an
ifstatement. It allows the program to make decisions between several possibilities. If the first condition is false, Python checks theelifcondition. This helps in writing clean and efficient decision-making programs.
What is the elif Statement?
The elif statement (short for else if) is used to check multiple conditions in sequence.
Python checks conditions from top to bottom
The first True condition is executed
Remaining conditions are skipped
If no condition is True, the else block runs (if present)
👉 The elif ladder avoids writing multiple separate if statements.
Why Use elif?
Use elif when:
There are more than two possible outcomes
Multiple conditions must be tested
Only one block should execute
Code needs to be clean and readable
Real-Life Examples
Syntax of elif Ladder
General Syntax
if condition1:
statement(s)
elif condition2:
statement(s)
elif condition3:
statement(s)
else:
statement(s)
Syntax Rules
if must come first
elif can be used multiple times
else is optional
Only one block executes
Indentation must be consistent
Indentation Rules
Each block under if, elif, and else must be indented
All blocks should align vertically
Python usually uses 4 spaces
Incorrect indentation leads to errors
Flow of Execution (How Python Works)
Python checks if condition
If False → checks first elif
Continues checking elif conditions
Executes first True block
Skips remaining conditions
If none True → executes else
Student Grading Using elif Ladder
This program assigns grades to students based on their marks using the elif ladder.
marks = 82
if marks >= 90:
print("Grade: A")
elif marks >= 75:
print("Grade: B")
elif marks >= 60:
print("Grade: C")
elif marks >= 35:
print("Grade: D")
else:
print("Fail")
Explanation
Conditions are checked from top to bottom
marks >= 75 becomes True first
Only Grade B is printed
Multiple Statements in elif
Each elif block can contain multiple statements.
Student Result Evaluation Using elif
This program prints result and remarks using multiple statements inside each block.
marks = 68
if marks >= 90:
print("Excellent performance")
print("Grade: A")
elif marks >= 60:
print("Good performance")
print("Grade: B")
else:
print("Needs improvement")
print("Grade: C")
Using Comparison Operators
Common operators used in elif:
Using String Conditions with elif
Traffic Signal Control Using elif
This program displays instructions based on traffic signal color.
signal = "yellow"
if signal == "red":
print("Stop")
elif signal == "yellow":
print("Get Ready")
elif signal == "green":
print("Go")
else:
print("Invalid signal")
User Access Level Using elif Ladder
This program determines user access level based on role value.
role = 2
if role == 1:
print("Admin Access")
elif role == 2:
print("Editor Access")
elif role == 3:
print("Viewer Access")
else:
print("No Access")
Where elif is Used?
Grading systems
Menu-driven programs
Role-based access
Status handling
Decision-making logic