Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Python if else

elif

In Python, elif (short for "else if") is a control flow keyword used within conditional statements. It allows you to create a series of checks following an initial if statement. If the if condition is False, the program proceeds to evaluate the first elif condition. If that elif condition is True, its corresponding code block is executed, and the rest of the elif and else statements are skipped.

Structure:

elif structure if condition1: # Code to execute if condition1 is True elif condition2: # Code to execute if condition1 is False and condition2 is True # ... (more elif statements can be added) else: # Code to execute if none of the above conditions are True
Points to Remember: ⮚ There can be zero or more elif statements after an if statement. ⮚ elif conditions are evaluated only if the preceding if and any previous elif conditions were False. ⮚ Only one elif block can be executed per conditional statement. ⮚ An else block is optional and executes if none of the if or elif conditions are True.

Examples:

Grade Evaluation

Finding grade by marks example using elif in python score = 85 if score >= 90: grade = "A" elif score >= 80: grade = "B" elif score >= 70: grade = "C" else: grade = "F" print(f"Your grade is: {grade}")

Output

Your grade is: B
In this example, if score is 85, the first if condition (score >= 90) is False, so the program moves to the first elif. Since score >= 80 is True, the grade is assigned "B" and the rest of the elif and else statements are skipped.

Day of the Week

Finding day in week using elif in python day = 3 if day == 1: day_name = "Monday" elif day == 2: day_name = "Tuesday" elif day == 3: day_name = "Wednesday" elif day == 4: day_name = "Thursday" elif day == 5: day_name = "Friday" elif day == 6: day_name = "Saturdar" elif day == 7: day_name = "Sunday" else: day_name = "Invalid day number" print(f"Today is: {day_name}")

Output

Today is: Wednesday
This code uses a chain of elif statements to map day numbers (1-7) to their corresponding day names. If day is not a valid number, the else block assigns "Invalid day number".

Discount Calculator (with Multiple elifs)

Calculating discount example using elif in python amount = 100 discount_rate = 0.1 # 10% discount if amount >= 200: discount = amount * discount_rate elif amount >= 150: discount = 15 # Fixed discount for amounts between 150 and 200 elif amount >= 100: discount = amount * 0.05 # 5% discount for amounts between 100 and 150 else: discount = 0 # No discount for amounts less than 100 final_price = amount - discount print(f"Final price after discount: {final_price}")

Output

Final price after discount: 95.0
This example demonstrates multiple elif conditions to apply different discount rates based on the purchase amount.

File Type Checker:

Checking file type example using if else in python extension = "jpg" if extension == "jpg" or extension == "jpeg": print("jpg/jpeg Image file") elif extension == "png": print("PNG image file") elif extension == "svg": print("svg image file") else: print("Unknown file type")

Output

jpg/jpeg Image file
This code uses elif to check for specific file extensions (JPEG, PNG, SVG) and prints the corresponding file type message.

Leap Year Checker

Leap year or not example using if else in python year = 2024 if year % 4 != 0: print("Not a leap year") elif year % 100 == 0 and year % 400 != 0: print("Not a leap year (century year not divisible by 400)") else: print("Leap year")

Output

Leap year
In this example, elif is used to handle the special case of century years (divisible by 100) that are not leap years unless also divisible by 400. When to Use elif: When you have multiple conditions to check in a single decision-making block. When you want to avoid nested if statements, which can make code harder to read and maintain. When you have a specific order in which you want to evaluate conditions. In Summary: elif provides a concise and readable way to handle multiple conditions in Python's conditional statements. By chaining if and elif statements, you can create complex decision-making logic that executes the appropriate code based on the evaluated conditions.

  📌TAGS

★python ★ if else ★ loop

Tutorials