Python - if , if..else, Nested if, if-elif statements
Last Updated :
07 Mar, 2025
There are situations in real life when we need to do some specific task and based on some specific conditions, we decide what we should do next. Similarly, there comes a situation in programming where a specific task is to be performed if a specific condition is True. In such cases, conditional statements can be used. The following are the conditional statements provided by Python.
- if
- if..else
- Nested if
- if-elif statements.
Let us go through all of them.
if Statement in Python
If the simple code of block is to be performed if the condition holds true then the if statement is used. Here the condition mentioned holds then the code of the block runs otherwise not.
Python if Statement Syntax
if condition:
# Statements to execute if condition is true
Flowchart of if Statement in Python
Below is the flowchart by which we can understand how to use if statement in Python:

Example: Basic Conditional Check with if Statement
In this example, an if
statement checks if 10 is greater than 5. If true, it prints "10 greater than 5"; regardless, it then prints "Program ended" as the next statement, indicating the program flow.
Python
# if statement example
if 10 > 5:
print("10 greater than 5")
print("Program ended")
Output10 greater than 5
Program ended
Indentation(White space) is used to delimit the block of code. As shown in the above example it is mandatory to use indentation in Python3 coding.
if else Statement in Python
In conditional if Statement the additional block of code is merged as else statement which is performed when if condition is false.
Python if-else Statement Syntax
if (condition):
# Executes this block if condition is true
else:
# Executes this block if condition is false
Flow Chart of if-else Statement in Python
Below is the flowchart by which we can understand how to use if-else statement in Python:

Example 1: Handling Conditional Scenarios with if-else
In this example, the code assigns the value 3 to variable x and uses an if..else statement to check if x is equal to 4. If true, it prints "Yes"; otherwise, it prints "No," demonstrating a conditional branching structure.
Python
# if..else statement example
x = 3
if x == 4:
print("Yes")
else:
print("No")
Example 2: Nested if..else Chain for Multiple Conditions
You can also chain if..else statement with more than one condition. In this example, the code uses a nested if..else chain to check the value of the variable letter. It prints a corresponding message based on whether letter is "B," "C," "A," or none of the specified values, illustrating a hierarchical conditional structure.
Python
# if..else chain statement
letter = "A"
if letter == "B":
print("letter is B")
else:
if letter == "C":
print("letter is C")
else:
if letter == "A":
print("letter is A")
else:
print("letter isn't A, B and C")
Nested if Statement
if statement can also be checked inside other if statement. This conditional statement is called a nested if statement. This means that inner if condition will be checked only if outer if condition is true and by this, we can see multiple conditions to be satisfied.
Python Nested If Statement Syntax
if (condition1):
# Executes when condition1 is true
if (condition2):
# Executes when condition2 is true
Flow chart of Nested If Statement In Python
Below is the flowchart by which we can understand how to use nested if statement in Python:

Example: Managing Nested Conditions for Refined Control
In this example, the code uses a nested if statement to check if the variable num is greater than 5. If true, it further checks if num is less than or equal to 15, printing "Bigger than 5" and "Between 5 and 15" accordingly, showcasing a hierarchical condition for refined control flow.
Python
# Nested if statement example
a = 10
if a > 5:
print("Bigger than 5")
if a <= 15:
print("Between 5 and 15")
OutputBigger than 5
Between 5 and 15
if-elif Statement in Python
The if-elif statement is shortcut of if..else chain. While using if-elif statement at the end else block is added which is performed if none of the above if-elif statement is true.
Python if-elif Statement Syntax
if (condition):
statement
elif (condition):
statement
else:
statement
Flow Chart of Python if-elif Statement
Below is the flowchart by which we can understand how to use elif in Python:

Sequential Evaluation with if-elif-else Structure
In this example, the code uses an if-elif-else statement to evaluate the value of the variable letter. It prints a corresponding message based on whether letter is "B," "C," "A," or none of the specified values, demonstrating a sequential evaluation of conditions for controlled branching.
Python
# if-elif statement example
letter = "A"
if letter == "B":
print("letter is B")
elif letter == "C":
print("letter is C")
elif letter == "A":
print("letter is A")
else:
print("letter isn't A, B or C")
Can We Use Elif in Nested If?
Yes, you can use elif
within nested if
statements in Python. This allows for more complex decision structures within a branch of another decision. For example:
Python
x = 10
y = 5
if x > 5:
if y > 5:
print("x is greater than 5")
elif y==5:
print("x is greater than 5 and y is 5")
else:
print("x is greater than 5 and y is less than 5")
Outputx is greater than 5 and y is 5
The structure of the above code provides conditional checks within another conditional check, enhancing the decision-making capabilities of your code.
Are You Allowed to Nest If Statements Inside Other If Statements in Python?
Yes, you are allowed to nest if statements inside other if statements in Python. This is a common practice used to make more complex conditional logic possible. Nested if statements can be as deep as you need, although deep nesting can make your code harder to read and maintain.
Similar Reads
Python Tutorial | Learn Python Programming Language
Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Python Interview Questions and Answers
Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts
Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced
Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions
Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs
Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Enumerate() in Python
enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read
Python Data Types
Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Python Introduction
Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Input and Output in Python
Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is
8 min read