Conditional flow in Python with if, elif, and else statements.
A program executes in Python line by line in a sequence. What if we only want to execute a code block when a condition is satisfied? In conditional execution, a code block will execute only if a condition is met. If not met, another block(optional).
If, Elif, and else are the statements for conditional flow in Python. Each conditional statement has an expression next to it. If the expression returns success, start execution just after the condition statement.

if expression evaluatioin :
#Code Under If condition if Expression
#Returns True
#Code That will always execute (OutSide of If block);
Example Conditional expression | Meaning |
---|---|
X == Y | True if X is equal to Y. |
X > Y | True if the X is Greater than Y; |
X != Y | True if X is not equal to Y. |
X < Y | True if X is less than Y. |
X <= Y | True if X is less than or equal to Y. |
X > Y | True if X is more than Y. |
X >= Y | True if X is more than or equal to Y. |
How does an if (elif or else) statement work?
- The expression next to the statement is evaluated.
- If its outcome is true (a non-zero value), the block under if block executes. Control reaches the block’s end if the outcome is false (a zero value).
- The indentation decides the scope of the block. The first line starts with indentation and ends with no indentation.
if expression evaluation :
#First Statement
#Second Statement
#.....
#Nth Statement
#Statement Not In Block Body
if expression evaluation : #outer if
if expression evaluation: #inner if
#First Statement (inner if)
#Second Statement(inner if)
#.....
#Nth Statement(inner if)
#Statements with in Fist if ((outer if)
#Statements Not in If condition
# Example Program for If statement
X = 10;
Y = 12;
if X < Y:
print("X Is smaller than Y");
print(" If Condition Checked (Always Printed)");
if isinstance(X, int):
print("The type of X is int");
print(" Type of X Checked (Always Printed)");
X Is smaller than Y
If Condition Checked (Always Printed)
The type of X is int
Type of X Checked (Always Printed)
- Initialized variable X to 10 and Y to 12.
- Check if X is smaller than Y. The expression return True.
- The body of
if
have only one statement [print(“X Is smaller than Y”)]. - Statement outside of the
if
body is executed. - Again Check if X is of type int. Returns true.
- Next, Statements are executed.
More flow control with if..else – Two-way decision
There could be an else with an if. So that if an expression is True, the body of the if block executes if not body of the else executes. It is used to make a binary decision. Following is the syntax of If..else.
if expression evaluation:
# First statement --
# Second statement --
# .....
# Nth statement --
else:
# First statement --
# Second statement --
# .....
# Nth statement --
#Statement always executes
- If the expression is true, the if block executes; otherwise, the else block executes.
- Indentation decides the statements for if and else.
#example program to check if an user
#entered number is
#positive or negative
print("Enter a Number");
num = int(input());
if num > 0:
print("Number is positive");
else:
print("Number is negative");
Output -> Enter a Number 3 Number is positive
Output-> Enter a Number -5 Number is negative
Multi-way decision with if..elif…else:
With the if..elif..else, you can choose a single flow from possible multiple flows. Unlike if..else, there could be multiple elif to performing decisions.
if conditional expression:
# First statement --
# Second statement --
# .....
# Nth statement --
elif conditional expression:
# First statement --
# Second statement --
# .....
# Nth statement --
elif conditional expression:
# First statement --
# Second statement --
# .....
# Nth statement --
else:
# First statement --
# Second statement --
# .....
# Nth statement --
#example program to check if an user
#entered number is
#zero , negative, more than 100 or more than 200
print("Enter a Number");
num = int(input());
if num == 0:
print("Number is zero");
elif num > 100 and num < 200:
print("Number is more than 100");
elif num > 200:
print("Number is more than 200");
else:
print("Number is negative");
Output-> Enter a Number 103 Number is more than 100
Output-> Enter a Number 202 Number is more than 200