The elif statement allows us to check multiple conditions.
and execute a block of code as soon as one of the conditions evaluates to TRUE.
The elif statement is optional, there can be an arbitrary number of elif statements following an if. The "elif" statement is a combination of the else and the if.
The elif Statement must come after an if statement and before a else.
Is it valid to have multiple elif statements proceeding one another.
if we need to make choice between more than two options then we can use if...elif...else statement.
if elif ... else Statement in Python
Syntax : if elif else statement in python
if condition1:
#execute this block
# if condition1 is true
elif condition2:
#execute this block
# if condition1 is false
elif condition3:
#execute this block
# if condition2 evaluates false
else:
#execute this block
# if all condition is false
The condition given in the above sysntax, Python always evaluates to either true or false. When if condition1: is true then the following code is executed i.e.
#execute this block
# if condition1 is true
if condition is not true then control jumps to elif condition2 and if the condition is true code inside elif condition2 block executes.
i.e.
#execute this block
# if condition1 is false
the process continues to rest of elif condition and finally the else code part executes when none of the above condtions evaluated or all conditions are false.
Given fig. show the syntax and flowchart of the if..elif... else statement in python.
lets learn how syntax (if...elif.. else statement) works from given Diagram.
Python starts script execution from top and goes to end.
After step by step successful execution of script the control
falls into the if statement.
after that
The flow jumps to Condition1.
and begins testing the Condition
1. if the condition tested is true. then
executes statements inside if body.
i.e. ''' #execute this block
# if condition is true'''
2. if the condition1 tested is false. then the control jumps to elif condition2 and test the condition if condtion evaluates true then the code inside elif executes i.e.
#execute this block
# if condition is true"
likewise the same process continues to rest of the elif statements.
When all the condition is false then statement inside else executes.
i.e. ''' Executes block if all conditions are false '''
Example 1: Python if...elif... else statement to check given number is positive,negative or zero.
p=8
if p<0: #test-condition
print(p," is Negative Number. ");
elif p>0:
print(q," is Positive Number. ")
else:
print("The Given number is Zero ")
Output:
8 is Positive Number.
The above program illustrates the use of if elif else statements to check Given number is postive,negative or zero.
1. In the above script, we have initialized variable p with value as 8
2. Then, we have used if p<0: with a test-expression to check the given number is small than 0 . We have used a relational operator < in if statement. Since the value of p is greater than 0, the condition becomes false and dose execute execute code inside body of if statement.
3. Then, control transfers to the elif block and test condition p>0: here p greateer than 0 which is true so executes the code or statements i.e. print(q," Positive Number") whic gives output as
8 is Positive Number.
if none of the conditions is true then else executes.
After that, the control goes outside of the block and script terminates with a successful result.
Example 2: Python script using if elif else to get the data type of a variable in python.
""" Python script using if elif else to get the data type of a variable in python. """
x = 3+6j
if (type(x) == int):
print("Data Type of the variable x is Integer")
elif (type(x) == float):
print("Data Type of the variable x is Float")
elif (type(x) == complex):
print("Data Type of the variable x is Complex")
elif (type(x) == bool):
print("Data Type of the variable x is Bool")
elif (type(x) == str):
print("Data Type of the variable x is String")
elif (type(x) == tuple):
print("Data Type of the variable x is Tuple")
elif (type(x) == dict):
print("Data Type of the variable x is Dictionaries")
elif (type(x) == list):
print("Data Type of the variable x is List")
else:
print("Unknown Data Type")
Output:
Data Type of the variable x is Complex
Previous Topic:-->> if else statement in Python || Next topic:-->>Nested if else in Python.