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