Control Statements in Python

3. if-else if - else Statement

When there are multiple conditions to check, you can use the elif clause (short for "else if"). Python evaluates each condition one by one until it finds one that is True. If none are True, the else block is executed.

Syntax:

 
if expression1 :
         statement_1
         statement_2
         ....   
   
     elif expression2 : 
     statement_3 
     statement_4
     ....     
   elif expression3 : 
     statement_5 
     statement_6
     ....................    
   else : 
     statement_7 
     statement_8

In the above case Python evaluates each expression (i.e. the condition) one by one and if a true condition is found the statement(s) block under that expression will be executed. If no true condition is found the statement(s) block under else will be executed. In the following example, we have applied if, series of elif and else to get the type of a variable.

var1 = 1+2j
if (type(var1) == int):
    print("Type of the variable is Integer")
elif (type(var1) == float):
    print("Type of the variable is Float")
elif (type(var1) == complex):
    print("Type of the variable is Complex")
elif (type(var1) == bool):
    print("Type of the variable is Bool")
elif (type(var1) == str):
    print("Type of the variable is String")
elif (type(var1) == tuple):
    print("Type of the variable is Tuple")
elif (type(var1) == dict):
    print("Type of the variable is Dictionaries")
elif (type(var1) == list):
    print("Type of the variable is List")
else:
    print("Type of the variable is Unknown")
 

Output:

Type of the variable is Complex

"Python if elif else" by w3resource is licensed under CC BY-SA 4.0