Nested Conditions in Python
In Python, you can use if
, elif
, and else
statements inside one another. This is known as nested conditions. Nested conditions allow you to check for multiple conditions in a structured and hierarchical manner, enabling more complex decision-making processes in your programs.
1. Basic Nested if
Statement
A simple nested if
statement is when one if
condition is placed inside another if
condition.
# Example 1: Basic Nested if statement
x = 10
if x > 5:
if x < 20:
print("x is between 5 and 20")
else:
print("x is greater than or equal to 20")
else:
print("x is less than or equal to 5")
Output:
x is between 5 and 20
2. Nested if-else
Statement
You can also combine if
and else
in nested conditions. This is useful when you want to check one condition and then handle multiple possibilities based on that condition.
# Example 2: Nested if-else statement
x = 15
if x > 10:
if x < 20:
print("x is between 10 and 20")
else:
print("x is greater than or equal to 20")
else:
print("x is less than or equal to 10")
Output:
x is between 10 and 20
3. Nested elif
Statement
Nested elif
statements allow you to check multiple conditions at once, with more than one layer of conditions evaluated in sequence.
# Example 3: Nested elif statement
x = 18
if x < 10:
print("x is less than 10")
elif x >= 10 and x < 20:
if x % 2 == 0:
print("x is between 10 and 20 and is even")
else:
print("x is between 10 and 20 and is odd")
else:
print("x is greater than or equal to 20")
Output:
x is between 10 and 20 and is even
4. Nested Conditions with Logical Operators
Logical operators such as and
, or
, and not
can be used inside nested conditions to combine multiple conditions within the same nested block.
# Example 4: Nested conditions with logical operators
age = 25
salary = 50000
if age > 18:
if salary >= 30000:
print("Eligible for loan")
else:
print("Salary is too low for loan eligibility")
else:
print("Age is less than 18, not eligible for loan")
Output:
Eligible for loan
5. Nested Conditions with Multiple Layers
It is possible to create multiple layers of nested conditions. However, be cautious, as excessive nesting can lead to complex and hard-to-maintain code. Here is an example of multiple nested conditions.
# Example 5: Multiple nested conditions
x = 12
y = 5
if x > 10:
if y > 3:
if x % 2 == 0:
print("x is even and greater than 10, y is greater than 3")
else:
print("x is odd and greater than 10, y is greater than 3")
else:
print("y is less than or equal to 3")
else:
print("x is less than or equal to 10")
Output:
x is even and greater than 10, y is greater than 3
6. Nested Conditions with Loops
You can also combine nested conditions with loops to evaluate conditions multiple times, such as checking multiple values in a list.
# Example 6: Nested conditions inside a loop
numbers = [5, 12, 8, 21]
for num in numbers:
if num > 10:
if num % 2 == 0:
print(f"{num} is even and greater than 10")
else:
print(f"{num} is odd and greater than 10")
else:
print(f"{num} is less than or equal to 10")
Output:
12 is even and greater than 10 8 is less than or equal to 10 21 is odd and greater than 10 5 is less than or equal to 10
7. Conclusion
Nested conditions in Python allow for more complex decision-making by testing multiple conditions in a structured and hierarchical way. While they are powerful, it is important to use them judiciously to keep the code readable and maintainable. By combining if
, else
, and elif
with logical operators and loops, you can build flexible and robust programs.