Python else if statement
So far, we have presented a Boolean option for conditional statements, with each if statement evaluating to either true or false. In many cases, we will want a program that evaluates more than two possible outcomes. For this, we will use an else if statement, which is written in Python as elif. The elif or else if statement looks like the if statement and will evaluate another condition.
Python Nested If Else Syntax :
If condition :
statements
elif condition:
statements
else:
statements
Python Nested If Else Example :
if grade >= 90:
print("A grade")
elif grade >=80:
print("B grade")
elif grade >=70:
print("C grade")
elif grade >= 65:
print("D grade")
else:
print("Failing grade")
Since elif statements will evaluate in order, we can keep our statements pretty basic. This program is completing the following steps:
If the grade is greater than 90, the program will print A grade, if the grade is less than 90, the program will continue to the next statement...
If the grade is greater than or equal to 80, the program will print B grade, if the grade is 79 or less, the program will continue to the next statement...
If the grade is greater than or equal to 70, the program will print C grade, if the grade is 69 or less, the program will continue to the next statement...
If the grade is greater than or equal to 65, the program will print D grade, if the grade is 64 or less, the program will continue to the next statement...
The program will print Failing grade because all of the above conditions were not met.
advertisement