Python If, If... Else, If... Elif... Else and Nested If Statement
Python If, If... Else, If... Elif... Else and Nested If Statement
Table of Contents
What are if...else statement in Python?
Python if Statement Syntax
Python if Statement Flowchart
Example: Python if Statement
if test expression:
statement(s)
Here, the program evaluates the test expression and will execute statement(s) only if the text
TUTORIAL EXAMPLES
expression is True .
BUILT-IN FUNCTIONS
In Python, the body of the if statement is indicated by the indentation. Body starts with an
indentation and the rst unindented line marks the end.
Python interprets non-zero values as True . None and 0 are interpreted as False .
Run
Powered by DataCamp
When variable num is equal to 3, test expression is true and body inside body of if is
executed.
If variable num is equal to -1, test expression is false and body inside body of if is skipped.
The print() statement falls outside of the if block (unindented). Hence, it is executed
regardless of the test expression.
if test expression:
Body of if
else:
Body of else
The if..else statement evaluates test expression and will execute body of if only when test
condition is True .
If the condition is False , body of else is executed. Indentation is used to separate the blocks.
Example of if...else
script.py IPython Shell
1 # Program checks if the number is positive or negative
2 # And displays an appropriate message
3
4 num = 3
5
6 # Try these two variations as well.
7 # num = -5
8 # num = 0
9
10 if num >= 0:
11 print("Positive or Zero")
12 else:
13 print("Negative number")
Run
Powered by DataCamp
In the above example, when num is equal to 3, the test expression is true and body of if is
executed and body of else is skipped.
If num is equal to -5, the test expression is false and body of else is executed and body of
if is skipped.
If num is equal to 0, the test expression is true and body of if is executed and body of else
is skipped.
The elif is short for else if. It allows us to check for multiple expressions.
If the condition for if is False , it checks the condition of the next elif block and so on.
Only one block among the several 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.
Flowchart of if...elif...else
Example of if...elif...else
TUTORIAL EXAMPLES BUILT-IN FUNCTIONS
script.py IPython Shell
1 # In this program,
2 # we check if the number is positive or
3 # negative or zero and
4 # display an appropriate message
5
6 num = 3.4
7
8 # Try these two variations as well:
9 # num = 0
10 # num = -4.5
11
12 if num > 0:
13 print("Positive number")
14 elif num == 0:
15 print("Zero")
16 else:
17 print("Negative number")
Run
Powered by DataCamp
Any number of these statements can be nested inside one another. Indentation is the only way
to gure out the level of nesting. This can get confusing, so must be avoided if we can.
Output 1
Enter a number: 5
Positive number
Output 2
Enter a number: -1
Negative number
Output 3
Enter a number: 0
Zero
PREVIOUS
PYTHON NAMESPACE AND SCOPE
NEXT
PYTHON FOR LOOP
Want to learn more Python for Data Science? Head over to DataCamp and try their
free Python Tutorial
TUTORIAL EXAMPLES BUILT-IN FUNCTIONS
Python Tutorial
Python Introduction
Python Flow Control
Python if...else
Python Pass
Take Quiz
Python Functions
Python Datatypes
Python Files
Python Object & Class
Advanced Topics
Subscribe
ABOUT
CONTACT
ADVERTISE
C PROGRAMMING
TUTORIAL EXAMPLES BUILT-IN FUNCTIONS
C++ PROGRAMMING
R PROGRAMMING