Python If...Else Statement
What is if...else statement?
The if...else
statement is a decision-making that is required when you want to execute a code only if a specific condition is fulfilled.
The if... elif...else
statement evaluates multiple expressions that produce TRUE or FALSE as a result. You need to determine which action to take and which statements to run if the result is TRUE or FALSE.
Python if Statement Syntax
The syntax of the if
statement can be given as follows:
if condition:
statement(s)
In the above syntax, the code evaluates the condition
and will run statement(s) only if the test expression is True
. Otherwise, if the condition is False
, the statement(s) is not executed.
The body of the if
statement is identified by the indentation. The body begins with an indentation and ends with the first unindented line marks.
Python interprets non-zero values as True
. The 0
and None
values are interpreted as False
.
Python if Statement Flowchart
The flowchart of the if
statement is as follows:

Python if Statement - Example
Let us consider the following example.
x = 10
if x > 5:
print(x, "is greater than 5.")
print("This will always be printed.")
x = 3
if x > 5:
print(x, "is greater than 5.")
print("This also will always be printed.")
The output of the above code is as follows:
10 is greater than 5.
This will always be printed.
This also will always be printed.
In the above code, the condition is x > 5
.
The body of the if
statement will be executed only if the condition evaluates to True
.
When the variable x
is equal to 10, the condition is true, and the code inside the body of if
is executed.
When the variable x
is equal to 3, the condition is false, and the code of the body of if
is skipped.
The print()
statements that are outside of the if
block. They are always printed regardless of the condition.
Python if...else Statement
Python if...else Syntax
The syntax of the if...else
can be given as follows:
if condition:
Body of if
else:
Body of else
The if...else
statement evaluates the condition and will run the body of the if
if the condition is True
. However, if the condition is False
, the body of else
is executed.
Indentation is used to separate the body of if
and else
.
Python if..else Statement Flowchart
The flowchart of the if...else
statement is as follows:

Python if...else Statement - Example
Let us see the following example.
x = 10
if x > 5:
print(x, "is greater than 5.")
else:
print(x, "is less or equal than 5.")
x = 3
if x > 5:
print(x, "is greater than 5.")
else:
print(x, "is less or equal to 5.")
After executing the above code, the output will be as follows:
10 is greater than 5.
3 is less or equal to 5.
Here above, when x
is equal to 5, the condition is true, and the body of if
is executed, and the body of else
is skipped.
When x
is equal to 3, the condition is false, and the body of if
is ignored, and the body of else
is executed.
Python if...elif...else Statement
Python if...elif...else Syntax
The syntax of the if...elif...else
can be given as follows:
if condition:
Body of if
elif condition:
Body of elif
else:
Body of else
The elif
is short for esle if
.
When the condition for if
is False
, it verifies the condition of the next elif
block and so on.
If all the conditions are False
, the body of else is executed.
Only one block of the if...elif...else
blocks is executed according to the condition.
The if
block can have only one else
block, but it can have multiple elif
blocks.
Python if..elif...else Statement Flowchart
The flowchart of the if...elif...else
statement is as follows:

Python if...elif...else Statement - Example
Let us consider the following example.
x = 3
if x > 5:
print(x, "is greater than 5.")
elif x == 5:
print(x, "is equal to 5.")
else:
print(x, "is less than 5.")
The output of the above code is as follows:
3 is less than 5.
Here in the above code, x
is equal to 3, so the body of the else
block is executed, "3 is less than 5".
If x
is equal to 5, then "5 is equal to 5" is printed.
If x
is equal to 10, then "10 is greater than 5" is printed.
Python Nested if Statements
In some situations, you want to check for another condition after a condition resolves to true. In this case, you can use the nested if statement.
In Python, you can use the if...elif...else
statement inside another if...elif...else
statement.
Indentation is the only way to figure out the level of nesting. It is recommended to avoid the use of if
nested statements unless necessary because they can reduce the clarity of the code.
Python Nested if - Example
Let us consider the following example where we will ask the user to enter a number. Based on the entered number, the program will output if it is equal, great, or less than 5.
x = int(input("Enter a number: "))
if x > 5:
print(x, "is greater than 5.")
elif x == 5:
print(x, "is equal to 5.")
else:
print(x, "is less than 5.")
After executing the above code, we can be in one of the three following scenarios:
Output 1:
Enter a number: 5
5 is equal to 5.
Output 2:
Enter a number: 26
26 is greater than 5.
Output 3:
Enter a number: 0
0 is less than 5.