Sometimes there are more than two possibilities and we need more than two branches. One way to express a computation like that is a chained conditional.
General Syntax of if..elif...else statement is
if TEST EXPRESSION1: STATEMENTS_A elif TEST EXPRESSION2: STATEMENTS_B else: STATEMENTS_C
The elif is short for else if. It allows us to check for multiple expressions.
If the condition for if is False, it checks the condition of the next elif block and so on. If all the conditions are False, body of else is executed.
Only one block among the several if...elif...else blocks is executed according to the condition. The if block can have only one else block. But it can have multiple elif blocks.
Each condition is checked in order. If the first is false, the next is checked, and so on. If one of them is true, the corresponding branch executes, and the statement ends. Even if more than one condition is true, only the first true branch executes.
time=17 #time=10, time=13, time=17, time=22 if time<12: print("Good Morning") elif time<15: print("Good Afternoon") elif time<20: print("Good Evening") else: print("Good Night")
Good Evening
When variable time is less than 12, Good Morning is printed. If time is less than 15, Good Afternoon is printed. If time is less than 20, Good Evening is printed. If all above conditions fails Good Night is printed.