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

Nested-if statement in Python

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.

Similar Reads

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

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

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

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