0% completed
The if-elif-else statement in Python is used when multiple conditions need to be checked. Instead of writing multiple if statements, we use elif (short for "else if") to check additional conditions. If the if condition is True, its block runs; otherwise, the program checks the elif conditions one by one. If none of the conditions are True, the else block executes.
if condition1: # Code executes if condition1 is True elif condition2: # Code executes if condition1 is False and condition2 is True else: # Code executes if none of the above conditions are True
if block is evaluated first.if is False, Python checks the elif conditions.True, the else block executes.temperature is set to 35.temperature >= 40 is False, so Python checks the next elif condition.marks is set to 75.marks >= 90, marks >= 80, and marks >= 70) are checked.marks >= 70 is True, that block executes, printing:
Grade: C
The if-elif-else statement is useful when multiple conditions need to be checked in sequence. Instead of writing separate if statements, elif helps create a structured flow where only one condition executes. If none of the if or elif conditions are met, the else block provides a default action.
This approach makes the code more readable and efficient, reducing unnecessary checks. It is commonly used in scenarios like grading systems, temperature levels, and decision-based applications.
.....
.....
.....