Open In App

Python3 – if , if..else, Nested if, if-elif statements

Last Updated : 08 May, 2024
Like Article

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

  1. if
  2. if..else
  3. Nested if
  4. 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

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.


# if statement example
if 10 > 5:
    print("10 greater than 5")
print("Program ended")


10 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  

Syntax: if (condition): # Executes this block if # condition is trueelse: # 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.


# if..else statement example
x = 3
if x == 4:



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.


# if..else chain statement
letter = "A"
if letter == "B":
    print("letter is B")
    if letter == "C":
        print("letter is C")
        if letter == "A":
            print("letter is A")
            print("letter isn't A, B and C")


letter is A

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

Syntax: if (condition1): # Executes when condition1 is true if (condition2): # Executes when condition2 is true # if Block is end here# if Block is end here

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.


# Nested if statement example
num = 10
if num > 5:
    print("Bigger than 5")
    if num <= 15:
        print("Between 5 and 15")


Bigger 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:-  

Syntax: if (condition): statementelif (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:


Example: 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.


# 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")
    print("letter isn't A, B or C")


letter is A

Similar Reads

How to use if, else &amp; elif in Python Lambda Functions
Lambda function can have multiple parameters but have only one expression. This one expression is evaluated and returned. Thus, We can use lambda functions as a function object. In this article, we will learn how to use if, else &amp; elif in Lambda Functions. Using if-else in lambda function The lambda function will return a value for every valida
2 min read
Avoiding elif and ELSE IF Ladder and Stairs Problem
This article focuses on discussing the elif and else if ladder and stairs problem and the solution for the same in the C and Python Programming languages. The ELIF and ELSE IF Ladder and Stairs ProblemThere are programmers who shy away from long sequences of IF, ELSE IF, ELSE IF, ELSE IF , etc. typically ending with a final ELSE clause. In language
6 min read
Python If Else Statements - Conditional Statements
In both real life and programming, decision-making is crucial. We often face situations where we need to make choices, and based on those choices, we determine our next actions. Similarly, in programming, we encounter scenarios where we must make decisions to control the flow of our code. Conditional statements in Python play a key role in determin
8 min read
Python | Check if a nested list is a subset of another nested list
Given two lists list1 and list2, check if list2 is a subset of list1 and return True or False accordingly. Examples: Input : list1 = [[2, 3, 1], [4, 5], [6, 8]] list2 = [[4, 5], [6, 8]] Output : True Input : list1 = [['a', 'b'], ['e'], ['c', 'd']] list2 = [['g']] Output : False Let's discuss few approaches to solve the problem. Approach #1 : Naive
7 min read
Arcade inbuilt functions to draw polygon in Python3
The arcade library is a high-tech Python Package with advanced set of tools for making 2D games with gripping graphics and sound. It is Object-oriented and is especially built for Python 3.6 and above versions. Arcade has two inbuilt functions for drawing a polygon: 1. arcade.draw_polygon_outline( ) : This function is used to draw the outline of th
2 min read
How to write Comments in Python3?
Comments are text notes added to the program to provide explanatory information about the source code. They are used in a programming language to document the program and remind programmers of what tricky things they just did with the code and also help the later generation for understanding and maintenance of code. The compiler considers these as
4 min read
Python3 Program for Check if an array is sorted and rotated
Given an array of N distinct integers. The task is to write a program to check if this array is sorted and rotated counter-clockwise. A sorted array is not considered as sorted and rotated, i.e., there should at least one rotation.Examples: Input : arr[] = { 3, 4, 5, 1, 2 } Output : YES The above array is sorted and rotated. Sorted array: {1, 2, 3,
3 min read
How to show a timer on screen using arcade in Python3?
Prerequisite: Arcade library Arcade is a modern framework, which is used to make 2D video games. In this, article, you will learn how to show an on-screen timer using the arcade module of Python. Displaying a timer on screen is not tough job, just follow the below steps:- Step 1: First of all import the arcade module of Python C/C++ Code Step 2: De
2 min read
Python2 vs Python3 | Syntax and performance Comparison
Python 2.x has been the most popular version for over a decade and a half. But now more and more people are switching to Python 3.x. Python3 is a lot better than Python2 and comes with many additional features. Also, Python 2.x is becoming obsolete this year. So, it is now recommended to start using Python 3.x from now-onwards. Still in dilemma? Ev
4 min read
How to install Python3 and PIP on Godaddy Server?
GoDaddy VPS is a shared server that provides computational services, databases, storage space, automated weekly backups, 99% uptime, and much more. It’s a cheaper alternative to some other popular cloud-based services such as AWS, GPC, and Azure. Python is an open-source, cross-platform, high-level, general-purpose programming language created by G
2 min read
Practice Tags :