In the previous lessons, you learned how to use the if statement to execute code when a condition is true and how the if...else statement allows a program to choose between two possible outcomes. However, many real-world problems involve more than two possible choices.
For example, consider the following situations:
In these situations, a simple if statement is not enough because it checks only one condition. An if...else statement handles only two possible outcomes. When there are multiple conditions to evaluate, Python provides the if...elif...else statement.
The elif keyword stands for “else if”. It allows Python to check another condition if the previous condition is false. You can use multiple elif blocks to test several conditions one after another.
The if...elif...else statement is one of the most powerful decision-making structures in Python. It helps programmers create applications that can respond differently depending on many possible situations.
After completing this lesson, you will be able to:
if...elif...else statement.if, if...else, and if...elif...else.elif blocks.if...elif...else Statement?Suppose you want to assign grades based on marks.
A simple if statement cannot handle all these possibilities. Likewise, an if...else statement provides only two choices. The if...elif...else statement allows Python to evaluate multiple conditions until it finds the first one that is true.
if...elif...else Statement?The if...elif...else statement is a conditional statement that allows Python to evaluate multiple conditions in sequence.
Python starts with the if condition. If it evaluates to True, the corresponding block executes and the remaining conditions are skipped.
If the first condition is False, Python checks the first elif condition. If that condition is also false, Python continues checking the next elif condition until one becomes true.
If none of the conditions are true, Python executes the else block.
if condition1:
statement1
elif condition2:
statement2
elif condition3:
statement3
else:
statement4
The statement consists of four parts:
if block checks the first condition.elif blocks check additional conditions.else block executes when no condition is true.if...elif...else Statement WorksPython evaluates the conditions one by one from top to bottom.
if condition.True, execute its block and stop checking the remaining conditions.False, evaluate the first elif condition.elif until one becomes True.else block.Only one block executes during each program run.
if...elif...else ProgramLet’s create a simple program that displays a grade based on marks.
marks = 82
if marks >= 90:
print("Grade A")
elif marks >= 80:
print("Grade B")
elif marks >= 70:
print("Grade C")
else:
print("Grade D")
Output
Grade B
Although marks >= 70 is also true, Python stops checking after finding the first true condition, which is marks >= 80.
number = 0
if number > 0:
print("Positive")
elif number < 0:
print("Negative")
else:
print("Zero")
Output
Zero
The first two conditions are false, so Python executes the else block.
if, if...else, and if...elif...else| Statement | Purpose |
|---|---|
if |
Executes code only when a condition is true. |
if...else |
Chooses between two possible outcomes. |
if...elif...else |
Chooses between multiple possible outcomes. |
Using if
age = 20
if age >= 18:
print("Adult")
Using if...else
age = 16
if age >= 18:
print("Adult")
else:
print("Minor")
Using if...elif...else
age = 65
if age < 13:
print("Child")
elif age < 20:
print("Teenager")
elif age < 60:
print("Adult")
else:
print("Senior Citizen")
Output
Senior Citizen
The if...elif...else statement follows the same indentation rules as other conditional statements in Python.
age = 25
if age < 18:
print("Minor")
elif age < 60:
print("Adult")
else:
print("Senior Citizen")
In this section, you learned why the Python if...elif...else statement is needed when a program must choose between multiple possible outcomes. You explored its syntax, execution flow, indentation rules, and the differences between if, if...else, and if...elif...else. You also created your first programs using multiple conditions and discovered that Python executes only the first block whose condition evaluates to True. In the next section, you will learn how to build more advanced programs using multiple elif blocks, Boolean values, user input, nested conditional statements, and practical examples.
The if...elif...else statement becomes more powerful when it is combined with comparison operators, Boolean expressions, variables, and user input. By combining these elements, you can create programs that evaluate several conditions and respond appropriately to different situations.
In this section, you will learn how to use multiple elif blocks, Boolean variables, nested conditional statements, and user input to build practical decision-making programs.
if...elif...elseComparison operators compare two values and return either True or False. These Boolean results determine which block of the if...elif...else statement will execute.
| Operator | Description | Example |
|---|---|---|
== |
Equal to | x == y |
!= |
Not equal to | x != y |
> |
Greater than | x > y |
< |
Less than | x < y |
>= |
Greater than or equal to | x >= y |
<= |
Less than or equal to | x <= y |
marks = 91
if marks >= 90:
print("Excellent")
elif marks >= 75:
print("Very Good")
elif marks >= 60:
print("Good")
else:
print("Needs Improvement")
Output
Excellent
Python checks the conditions one by one and stops as soon as it finds the first condition that evaluates to True.
temperature = 28
if temperature >= 35:
print("Hot")
elif temperature >= 25:
print("Warm")
elif temperature >= 15:
print("Cool")
else:
print("Cold")
Output
Warm
elif BlocksYou can use as many elif blocks as your program requires. Python evaluates each condition from top to bottom until one condition becomes true.
day = 6
if day == 1:
print("Monday")
elif day == 2:
print("Tuesday")
elif day == 3:
print("Wednesday")
elif day == 4:
print("Thursday")
elif day == 5:
print("Friday")
elif day == 6:
print("Saturday")
elif day == 7:
print("Sunday")
else:
print("Invalid Day")
Output
Saturday
This example demonstrates how multiple elif blocks allow a program to handle many possible outcomes.
Boolean variables can also be used inside an if...elif...else statement.
is_member = True
purchase_amount = 1500
if is_member:
print("Member Discount Applied")
elif purchase_amount > 1000:
print("Festival Discount Applied")
else:
print("No Discount Available")
Output
Member Discount Applied
The first condition is already true, so Python does not evaluate the remaining conditions.
Most interactive programs use user input before making decisions.
age = int(input("Enter your age: "))
if age < 13:
print("Child")
elif age < 20:
print("Teenager")
elif age < 60:
print("Adult")
else:
print("Senior Citizen")
Sample Output
Enter your age: 18
Teenager
The program categorizes the user according to the entered age.
if...elif...else StatementsYou can place one conditional statement inside another. This technique is called a nested if...elif...else statement.
Nested conditions are useful when one decision depends on another.
age = 22
has_license = True
if age >= 18:
if has_license:
print("You can drive.")
else:
print("Apply for a driving license.")
else:
print("You are too young to drive.")
Output
You can drive.
Python first checks the age. If that condition is true, it evaluates the nested condition.
Consider the following program:
score = 72
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")
Execution Steps
score >= 90. The result is False.score >= 80. The result is False.score >= 70. The result is True.Output
Grade C
Incorrect
marks = 95
if marks >= 60:
print("Grade D")
elif marks >= 90:
print("Grade A")
This program prints Grade D because the first condition is already true.
Correct
marks = 95
if marks >= 90:
print("Grade A")
elif marks >= 60:
print("Grade D")
if marks >= 90
print("Grade A")
This produces a SyntaxError.
Every block must be properly indented using four spaces.
= Instead of ==Always use == when comparing values inside conditions.
In this section, you learned how to build more advanced decision-making programs using the Python if...elif...else statement. You explored comparison operators, multiple elif blocks, Boolean variables, nested conditional statements, and user input. You also learned how Python evaluates conditions sequentially and why the order of conditions is important. Finally, you reviewed common beginner mistakes such as incorrect condition ordering, missing colons, improper indentation, and using the wrong comparison operator. In the next section, you will apply these concepts through practical programs such as grade calculators, traffic signal simulations, menu-driven applications, and age classification systems.
The true value of the if...elif...else statement becomes clear when it is used to solve real-world programming problems. Many applications need to choose between several possible actions instead of just two. Whether you are assigning grades, displaying menu options, calculating discounts, or categorizing users, the if...elif...else statement provides a clean and readable solution.
In this section, you will build several practical programs that demonstrate how multiple conditions are evaluated in Python. These examples will help you understand how to apply conditional logic to everyday programming tasks.
if...elif...else StatementThis program assigns a grade based on the student's marks.
marks = 86
if marks >= 90:
print("Grade A")
elif marks >= 80:
print("Grade B")
elif marks >= 70:
print("Grade C")
elif marks >= 60:
print("Grade D")
else:
print("Grade F")
Output
Grade B
Python evaluates each condition from top to bottom and stops when it finds the first condition that is true.
This program displays the appropriate action based on the traffic signal.
signal = "Yellow"
if signal == "Red":
print("Stop")
elif signal == "Yellow":
print("Get Ready")
elif signal == "Green":
print("Go")
else:
print("Invalid Signal")
Output
Get Ready
a = 25
b = 40
c = 18
if a >= b and a >= c:
print("Largest:", a)
elif b >= a and b >= c:
print("Largest:", b)
else:
print("Largest:", c)
Output
Largest: 40
This example combines comparison operators with logical operators to compare multiple values.
age = 67
if age < 13:
print("Child")
elif age < 20:
print("Teenager")
elif age < 60:
print("Adult")
else:
print("Senior Citizen")
Output
Senior Citizen
month = 4
if month == 1:
print("January")
elif month == 2:
print("February")
elif month == 3:
print("March")
elif month == 4:
print("April")
else:
print("Month Not Available")
Output
April
Many applications display a menu and allow users to choose an option. The if...elif...else statement is commonly used to implement such menus.
choice = int(input("Choose an option (1-3): "))
if choice == 1:
print("Create Account")
elif choice == 2:
print("Login")
elif choice == 3:
print("Exit")
else:
print("Invalid Choice")
Sample Output
Choose an option (1-3): 2
Login
This program checks the selected option and performs the corresponding action.
User input makes programs interactive by allowing users to provide values during execution.
amount = float(input("Enter purchase amount: "))
if amount >= 5000:
print("20% Discount")
elif amount >= 3000:
print("10% Discount")
elif amount >= 1000:
print("5% Discount")
else:
print("No Discount")
Sample Output
Enter purchase amount: 3500
10% Discount
The program evaluates each condition until it finds the first matching discount category.
if...elif...elseAlways place the most specific or highest-priority conditions before more general conditions.
Correct Example
marks = 95
if marks >= 90:
print("Grade A")
elif marks >= 80:
print("Grade B")
else:
print("Grade C")
Simple conditions make your programs easier to understand and maintain.
student_marks = 82
if student_marks >= 80:
print("Excellent")
Each condition should check a different possibility. Repeated conditions make the code harder to read and may produce incorrect results.
else Block for Default CasesThe else block should handle unexpected or remaining cases that are not covered by the previous conditions.
= instead of == in comparisons.:) after if, elif, or else.elif conditions that can never be reached.marks = 95
if marks >= 60:
print("Pass")
elif marks >= 90:
print("Excellent")
Since the first condition is already true, the second condition will never be checked.
marks = 95
if marks >= 90:
print("Excellent")
elif marks >= 60:
print("Pass")
else:
print("Fail")
In this section, you learned how to apply the Python if...elif...else statement to practical programming problems. You created programs such as a student grade calculator, traffic signal simulator, largest-of-three-number checker, age category classifier, month name selector, menu-driven application, and purchase discount calculator. You also explored best practices for organizing conditions and reviewed common mistakes that beginners often make. In the final section, you will review the lesson through a summary, key takeaways, frequently asked questions, coding exercises, interview questions, a mini project, and a preview of the next lesson on Nested if Statements.
In this lesson, you learned how the Python if...elif...else statement allows a program to evaluate multiple conditions and execute the appropriate block of code. Unlike the if statement, which handles only one condition, and the if...else statement, which handles two possible outcomes, the if...elif...else statement is designed for situations where several choices are possible.
You explored the syntax, execution flow, and indentation rules of the if...elif...else statement. You learned how Python evaluates conditions from top to bottom and executes only the first block whose condition is True. You also used comparison operators, Boolean variables, nested conditional statements, and user input to create interactive programs.
Through practical examples such as grade calculators, traffic signal programs, age classification systems, and menu-driven applications, you gained experience in applying multiple conditional statements to solve real-world programming problems.
if...elif...else statement is used when there are multiple possible outcomes.True is executed.elif conditions are skipped after a match is found.else block executes only if none of the previous conditions are true.if...elif...else statement is widely used in real-world applications.if...elif...else statement?It allows a program to evaluate multiple conditions and execute the appropriate block of code.
elif mean?elif stands for "else if". It checks another condition if the previous condition is false.
elif blocks?Yes. A Python program can contain as many elif blocks as required.
elif condition?No. Python stops checking conditions as soon as it finds the first condition that evaluates to True.
else block mandatory?No. The else block is optional, but it is useful for handling all remaining cases.
if statement inside an elif block?Yes. This is called a nested conditional statement.
If a general condition appears before a specific one, Python may execute the general condition first, making the specific condition unreachable.
if...elif...else statement commonly used?It is commonly used in grading systems, menu-driven programs, login systems, discount calculations, and many other applications that require multiple decision paths.
if...elif...else statement?elif keyword?elif blocks execute during the same program run?if, if...else, and if...elif...else?if...elif...else statement.Create a Python program that calculates and displays a student's grade based on marks.
Your program should:
Enter Student Name: Rahul
Enter Marks: 86
Student Name: Rahul
Marks: 86
Grade: B
Program Completed.
Congratulations! You now understand how the Python if...elif...else statement evaluates multiple conditions and selects the appropriate block of code. This structure is one of the most commonly used decision-making tools in Python and forms the basis of many real-world applications.
In the next lesson, you will learn Nested if Statements. You will explore how one conditional statement can be placed inside another, allowing you to build more advanced decision-making logic for applications such as login systems, admission eligibility, banking operations, and access control.