Understanding Nested If-Else in Python
This article is a complementary resource to the Learn Python Basics course.
This article is a complementary resource to the Learn Python Basics course.
Understanding Nested If-Else in Python
A nested if...else statement is an if...else statement placed inside another one. For example,
# Program to check if a number is even, odd, or negative
number = int(input("Enter a number: "))
# Outer if...else statement
if number >= 0:
# Inner if...else statement
if number % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")
else:
print("The number is negative.")
Output
Enter a number: 10 The number is even.
Here's how the above program works:
Outer if...else statement:
number >= 0).True, it moves to the inner if...else statement.False, it runs the else block and prints The number is negative.Inner if...else statement:
number % 2 == 0).True, it prints The number is even.The number is odd.Note: Python decides which statements belong to which block by indentation. The inner if...else is indented one level further than the outer one, so it only runs when the outer condition is True.
Nested if...else statements are not bad in themselves, but they can make your code harder to read and maintain. For example,
# Program to find the largest of three numbers
number1 = int(input("Enter the first number: "))
number2 = int(input("Enter the second number: "))
number3 = int(input("Enter the third number: "))
if number1 > number2:
if number1 > number3:
print(f"The largest number is {number1}")
else:
print(f"The largest number is {number3}")
else:
if number2 > number3:
print(f"The largest number is {number2}")
else:
print(f"The largest number is {number3}")
Output
Enter the first number: 3 Enter the second number: 9 Enter the third number: 5 The largest number is 9
The program works as expected. But you have to follow two levels of conditions to see which branch prints the answer.
Now imagine extending the same logic to find the largest of four numbers. Every extra number adds another level of nesting. For example,
# Program to find the largest of four numbers
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
num3 = int(input("Enter the third number: "))
num4 = int(input("Enter the fourth number: "))
if num1 > num2:
if num1 > num3:
if num1 > num4:
print(f"The largest number is {num1}")
else:
print(f"The largest number is {num4}")
else:
if num3 > num4:
print(f"The largest number is {num3}")
else:
print(f"The largest number is {num4}")
else:
if num2 > num3:
if num2 > num4:
print(f"The largest number is {num2}")
else:
print(f"The largest number is {num4}")
else:
if num3 > num4:
print(f"The largest number is {num3}")
else:
print(f"The largest number is {num4}")
The code still gives the right answer, but it is now three levels deep and much harder to check by eye.
You can often remove nesting by combining conditions with the logical operators and and or, and by using elif for the remaining cases.
For example, here's a flatter way to find the largest of three numbers:
# Program to find the largest of three numbers
number1 = int(input("Enter the first number: "))
number2 = int(input("Enter the second number: "))
number3 = int(input("Enter the third number: "))
if number1 > number2 and number1 > number3:
print(f"The largest number is {number1}")
elif number2 > number3:
print(f"The largest number is {number2}")
else:
print(f"The largest number is {number3}")
Here, the and operator checks both comparisons in one condition, and elif handles the next case without another level of indentation. The result is the same, and each branch reads in one line.
if...else statement handles conditions that depend on an earlier condition.and and or, together with elif, keep it flat.