1The elif statement allows you to check multiple expressions for TRUE
2and execute a block of code as soon as one of the conditions evaluates
3to TRUE. Similar to the else, the elif statement is optional. However,
4unlike else, for which there can be at most one statement, there can
5be an arbitrary number of elif statements following an if.
6
7if expression1:
8 statement(s)
9elif expression2:
10 statement(s)
11elif expression3:
12 statement(s)
13else:
14 statement(s)
15
1num = 20
2if num > 30:
3 print("big")
4elif num == 30:
5 print("same")
6else:
7 print("small")
8#output: small
1if num > 0:
2 print("Positive number")
3elif num == 0:
4 print("Zero")
5else:
6 print("Negative number")
1def function(a):
2 if a == '1':
3 print ('1a')
4 elif a == '2':
5 print ('2a')
6 else:
7 print ('3a')
8
1>>> a = 5
2>>> if a > 5:
3... a = a + 1
4... elif a == 5:
5... a = a + 1000
6... else:
7... a = a - 1
8...
9>>> a
101005
11