Python Nested if Statement
A nested if statement means an if statement placed
inside another if statement. It is used when a second
condition needs to be checked only after the first condition is true.
Nested if statements are useful when a program has
multiple levels of decision making.
Syntax of Nested if Statement
if condition1:
if condition2:
statement
Here, the inner if statement is executed only when the
outer if condition is True.
Simple Nested if Example
age = 25
if age >= 18:
if age <= 60:
print("Age is between 18 and 60")
Age is between 18 and 60
First, the outer condition age >= 18 is checked.
If it is true, the inner condition age <= 60 is checked.
Nested if with User Input
age = int(input("Enter your age: "))
if age >= 18:
if age <= 60:
print("You are an adult.")
Enter your age: 25 You are an adult.
The inner condition is checked only when the user enters an age of 18 or above.
Nested if with else
An else block can be used with the inner
if statement.
age = 20
if age >= 18:
if age >= 21:
print("Eligible")
else:
print("Adult but below 21")
else:
print("Below 18")
Adult but below 21
The outer condition checks whether the person is at least 18. The inner condition then checks whether the age is at least 21.
Nested if for Checking Number
number = 20
if number > 0:
if number % 2 == 0:
print("Positive even number")
Positive even number
The first condition checks whether the number is positive. If it is positive, the second condition checks whether it is even.
Nested if for Positive and Negative Numbers
number = -10
if number != 0:
if number > 0:
print("Positive number")
else:
print("Negative number")
else:
print("Zero")
Negative number
The outer condition first checks whether the number is not zero. The inner condition then determines whether it is positive or negative.
Nested if for Student Result
marks = 75
if marks >= 40:
if marks >= 75:
print("Pass with distinction")
else:
print("Pass")
else:
print("Fail")
Pass with distinction
The outer if checks whether the student has passed.
If the student passes, the inner if checks for distinction.
Nested if with Multiple Conditions
age = 25
has_id = True
if age >= 18:
if has_id:
print("Entry allowed.")
else:
print("ID required.")
else:
print("Age restriction applies.")
Entry allowed.
The second condition is checked only after the age condition is satisfied.
Nested if with String
username = "admin"
password = "python123"
if username == "admin":
if password == "python123":
print("Login successful")
else:
print("Incorrect password")
else:
print("Invalid username")
Login successful
The password is checked only when the username is correct.
Nested if with Membership Operator
fruits = ["apple", "banana", "mango"]
if "mango" in fruits:
if len(fruits) >= 3:
print("Mango is available and the list has at least 3 fruits.")
Mango is available and the list has at least 3 fruits.
Nested if with Comparison Operators
a = 50
b = 30
if a > b:
if a > 40:
print("a is greater than b and 40")
a is greater than b and 40
Nested if with Logical Operators
Logical operators can also be used inside nested conditions.
age = 25
salary = 40000
if age >= 18:
if salary >= 30000 and salary <= 100000:
print("Eligible")
Eligible
Nested if with Three Levels
Python allows an if statement to be nested inside another
nested if statement.
age = 25
has_id = True
has_ticket = True
if age >= 18:
if has_id:
if has_ticket:
print("Entry allowed")
else:
print("Ticket required")
else:
print("ID required")
else:
print("Not eligible")
Entry allowed
Here, three conditions are checked one after another. Each inner condition is checked only when the previous condition is true.
Nested if with Multiple Statements
marks = 85
if marks >= 40:
print("Student has passed.")
if marks >= 75:
print("Student has scored distinction.")
print("Excellent performance.")
Student has passed. Student has scored distinction. Excellent performance.
Importance of Indentation in Nested if
Indentation is especially important in nested if
statements because it determines which statements belong to the
outer and inner blocks.
age = 25
if age >= 18:
print("Adult")
if age >= 21:
print("Age is 21 or above")
Adult Age is 21 or above
IndentationError
or change the logical structure of the program.
Nested if vs if-else
| Nested if | if-else |
|---|---|
| Contains an if statement inside another if statement. | Contains an if block and an alternative else block. |
| Used for multiple levels of decision making. | Used when two alternative paths are required. |
| The inner condition depends on the outer condition. | The else block executes when the if condition is false. |
Practical Example: ATM Access
card_inserted = True
pin_correct = True
if card_inserted:
if pin_correct:
print("Access granted")
else:
print("Incorrect PIN")
else:
print("Please insert your card")
Access granted
Practical Example: Admission Eligibility
marks = 75
age = 19
if marks >= 60:
if age >= 18:
print("Eligible for admission")
else:
print("Age requirement not satisfied")
else:
print("Marks requirement not satisfied")
Eligible for admission
Important Points
- A nested
ifis anifstatement inside anotherifstatement. - The inner condition is checked only when the outer condition is true.
- Nested
ifstatements are useful for multiple levels of decision making. - Proper indentation is required for nested blocks.
- An
elsecan be associated with the inner or outerif. - Comparison and logical operators can be used in nested conditions.
- Too many levels of nesting can make a program difficult to read.
if statement allows Python to make a decision
inside another decision. The inner condition is evaluated only when
the outer condition is satisfied.