if and elif in python code example
Example 1: elif python
The elif statement allows you to check multiple expressions for TRUE
and execute a block of code as soon as one of the conditions evaluates
to TRUE. Similar to the else, the elif statement is optional. However,
unlike else, for which there can be at most one statement, there can
be an arbitrary number of elif statements following an if.
if expression1:
statement(s)
elif expression2:
statement(s)
elif expression3:
statement(s)
else:
statement(s)
Example 2: if statement in python
#Conditionals statements in python
#'=' conditionals statements
a = 123
b = 123
if(a==b):
print('True')
#<, > conditionals statements
a = 2
b = 45
if(a<b):
print('A is smaller than B')
Example 3: else if python
if expression1:
statement(s)
elif expression2:
statement(s)
elif expression3:
statement(s)
else:
statement(s)
Example 4: python if statement
if (condition1):
print('condition1 is True')
elif (condition2):
print('condition2 is True')
else:
print('None of the conditions are True')
Example 5: python if elif
num = 20
if num > 30:
print("big")
elif num == 30:
print("same")
else:
print("small")
#output: small
Example 6: python else elif
>>> a = 5
>>> if a > 5:
... a = a + 1
... elif a == 5:
... a = a + 1000
... else:
... a = a - 1
...
>>> a
1005