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.