Open In App

Nested-if statement in Python

Last Updated : 08 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

There comes a situation in real life when we need to make some decisions and based on these decisions, we decide what we should do next. Similar situations arise in programming also where we need to make some decisions and based on these decisions we choose when to execute the next block of code. This is done with the help of a Nested if statement.

Python Nested if Statement

In Python a Nested if statement, we can have an if…elif…else statement inside another if…elif…else statement. This is called nesting in computer programming. Any number of these statements can be nested inside one another. Indentation is the only way to figure out the level of nesting. This can get confusing, so it must be avoided if we can.

Flow Chart of Nested if Statement

Python nested if

We can use a simple Python if statement inside another if or if…else statement. It can be used to check multiple if statements. In this case, an if condition is present inside another if conditions. We can have multiple if conditions inside an if condition. The inner if condition will only be executed if and only if the outer if condition is True.

Python if Statement

In this example, first we will check if a number is not equal to zero using the if condition. If it returns True, then we will check if the number is positive, that is, the number is greater than 0.

Syntax

if(condition):
{
    // if body
    // Statements to execute if condition is true
}
Python
# Python code to demonstrate the syntax of if statement

gfg = 9

# if statement with true condition
if gfg < 10:
    print(f"{gfg} is less than 10")

# if statement with false condition
if gfg > 20:
    print(f"{gfg} is greater than 20")

Output:

9 is less than 10

Multiple IF Statements in Python

In this example, we will see how we can use multiple if statements nested inside a single if statement to check multiple conditions. We will take the previous example, but this time we will check for two conditions where the number is positive or negative.

Syntax

if (condition1):
    # executes when condition is True
    if (condition2):
        # executes when condition is True
Python
# Python program to demonstrate 
# nested if with multiple if statements

i = -15; 
# condition 1
if i != 0:
    # condition 2
    if i > 0:
        print("Positive")
    # condition 3
    if i < 0:
        print("Negative")

Output:

Negative

Nested if Statement With else Condition

We can also nest an if statement inside and if else statement in Python. In this example, we will first check if the number is not equal to 0. If it returns True, then we will check if the number if Positive or Negative. If the first If condition returns False, its code block will not execute and the else part of the Python if else statement will execute.

Python
i = 0; 

# if condition 1
if i != 0:
    # condition 1
    if i > 0:
        print("Positive")
        
    # condition 2
    if i < 0:
        print("Negative")
else:
    print("Zero")
    

Output:

Zero

Similar Reads

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
How to write an empty function in Python - pass statement?
In C/C++ and Java, we can write empty function as following // An empty function in C/C++/Java void fun() { } In Python, if we write something like following in Python, it would produce compiler error. # Incorrect empty function in Python def fun(): Output : IndentationError: expected an indented block In Python, to write empty functions, we use pa
1 min read
Using Else Conditional Statement With For loop in Python
Using else conditional statement with for loop in python In most of the programming languages (C/C++, Java, etc), the use of else statement has been restricted with the if conditional statements. But Python also allows us to use the else condition with for loops. The else block just after for/while is executed only when the loop is NOT terminated b
2 min read
Statement, Indentation and Comment in Python
Here, we will discuss Statements in Python, Indentation in Python, and Comments in Python. We will also discuss different rules and examples for Python Statement, Python Indentation, Python Comment, and the Difference Between 'Docstrings' and 'Multi-line Comments. What is Statement in Python A Python statement is an instruction that the Python inte
7 min read
Python return statement
A return statement is used to end the execution of the function call and "returns" the result (value of the expression following the return keyword) to the caller. The statements after the return statements are not executed. If the return statement is without any expression, then the special value None is returned. A return statement is overall use
4 min read
Python Continue Statement
Python Continue Statement skips the execution of the program block after the continue statement and forces the control to start the next iteration. Python Continue StatementPython Continue statement is a loop control statement that forces to execute the next iteration of the loop while skipping the rest of the code inside the loop for the current i
4 min read
Python break statement
Python break is used to terminate the execution of the loop. Python break statement Syntax:Loop{ Condition: break }Python break statement break statement in Python is used to bring the control out of the loop when some external condition is triggered. break statement is put inside the loop body (generally after if condition). It terminates the curr
3 min read
Check multiple conditions in if statement - Python
If-else conditional statement is used in Python when a situation leads to two conditions and one of them should hold true. Syntax: if (condition): code1 else: code2 [on_true] if [expression] else [on_false] Note: For more information, refer to Decision Making in Python (if , if..else, Nested if, if-elif) Multiple conditions in if statement Here we'
3 min read
How to Use IF Statement in MySQL Using Python
Prerequisite: Python: MySQL Create Table In this article, we are going to see how to use if statements in MySQL using Python. Python allows the integration of a wide range of database servers with applications. A database interface is required to access a database from Python. MySQL Connector-Python module is an API in python for communicating with
2 min read
How to Execute a SQLite Statement in Python?
In this article, we are going to see how to execute SQLite statements using Python. We are going to execute how to create a table in a database, insert records and display data present in the table. In order to execute an SQLite script in python, we will use the execute() method with connect() object: connection_object.execute("sql statement") Appr
2 min read
Article Tags :
Practice Tags :