An if-elif-else statement checks multiple conditions one by one. The program executes the block of code under the first condition that is true. If none of the conditions are true, the code block under the else statement is executed.
How It Works
- If Condition: The program first evaluates the condition following the if
- Elif Condition: If the if condition is false, the program evaluates the condition following the elif You can have multiple elif conditions.
- Else Condition: If none of the if or elif conditions are true, the program executes the code block under the else
Here’s the basic syntax of an if-elif-else statement in Python:
if condition1:
# code to execute if condition1 is true
elif condition2:
# code to execute if condition1 is false and condition2 is true
else:
# code to execute if none of the above conditions are true
Let's look at an example to understand how the if-elif-else statement works:
a = 20 b = 20 if b > a: print("b is greater than a") elif a == b: print("a and b are equal") else: print("b is less than a") |
- Variables: We have two variables, a and b, both set to 20.
- If Condition: The condition b > a is evaluated. Since b is not greater than a (both are equal), this condition is false, and the program moves to the next condition.
- Elif Condition: The condition a == b is evaluated. Since a equals b, this condition is true, and the program prints "a and b are equal".
- Else Condition: If neither of the above conditions were true, the program would execute the code block under else. However, in this case, the elif condition was true, so the else block is not executed.
The if-elif-else structure allows the program to handle multiple conditions gracefully, executing only the first true condition's code block.
Importance of Indentation
As with other Python control structures, proper indentation is crucial for if-elif-else statements. Python uses indentation to define the scope of code blocks.
Example:
Problem Statement: Write a Python program that takes a numerical grade input from the user and categorizes it into a letter grade based on the following criteria:
- A: 90-100
- B: 80-89
- C: 70-79
- D: 60-69
- F: 0-59
Instructions:
- Prompt the user to enter a numerical grade.
- Convert the input to an integer.
- Use an if-elif-else statement to categorize the grade into A, B, C, D, or F.
- Print the corresponding letter grade.
Example Output:
Enter a numerical grade: 85
The letter grade is B
Enter a numerical grade: 72
The letter grade is C
Solution:
# Step 1: Prompt the user to enter a numerical grade
|
Explanation:
- User Input: The program asks the user to input a numerical grade.
- Conversion: The input is then converted to an integer using int().
- Condition Check:
- The if statement checks if the grade is between 90 and 100 (inclusive) and assigns the letter grade 'A'.
- The elif statements check for the ranges 80-89, 70-79, and 60-69, assigning the letter grades 'B', 'C', and 'D' respectively.
- The else statement covers all grades below 60, assigning the letter grade 'F'.
- Print Statement: The program prints the corresponding letter grade based on the evaluated conditions.