Now, with the Python Nested If Statement, we can manage complex conditions by placing one decision structure inside another for better logical control. However, real-world logic often involves multiple layers. Sometimes, a decision cannot be made until a previous condition has already been met.
For example, on an e-learning platform like AI Learner Tech:
“First, check if a student is logged in. If they are, check their subscription tier. If they are a premium member, allow them to download the advanced Python notes.”
Understanding the Structure of Nesting #
When you nest conditional statements, you create a hierarchy of conditions. The outer condition acts as a primary gatekeeper. If the outer condition evaluates to False, Python completely skips everything inside it, including any inner conditions.
The inner condition only gets evaluated if the outer condition evaluates to True. This allows you to write highly specific, localized logic that only applies under certain circumstances.

Rules of Syntax and Proper Indentation #
Since Python relies on spacing to understand code structure, indentation is absolutely critical when working with nested statements. Each level of nesting requires its own extra layer of indentation.
if outer_condition:
# Code block for outer if
if inner_condition:
# Code block for inner if
print("Both conditions are True!")
The outer if statement starts at the left margin. The code that belongs to this outer if is indented by four spaces. When you place a new if statement inside that indented block, it introduces a second level of logic.
The code that belongs to the inner if statement must be indented by another four spaces, making a total of eight spaces from the left margin. If you misalign these spaces, Python will either throw an IndentationError or execute your code under the wrong condition.
Python Nested If Statement Explained #
Let’s explore practical, step-by-step examples that illustrate how to control the flow of nested decisions.
Example: Basic Account Activation Check #
In this beginner example, we check if a user’s account is active before checking if they have sufficient administrative privileges to delete a post.
is_account_active = True
is_admin = False
if is_account_active:
print("System Check: Account is active.")
if is_admin:
print("Action Approved: You have permission to delete this post.")
else:
print("Action Denied: You must be an administrator to perform this action.")
else:
print("Access Revoked: Your account is suspended.")
Output: #
System Check: Account is active. Action Denied: You must be an administrator to perform this action.
Example: Advanced Student Exam Eligibility #
Let’s build a program that evaluates if a student is eligible for an exam. This requires checking their attendance first, and then checking if they have submitted all assignments on time.
attendance_score = 85
assignments_submitted = True
if attendance_score >= 75:
print("Attendance check passed.")
if assignments_submitted:
print("Assignment check passed. You are eligible to download your exam admit card.")
else:
print("Assignment check failed. Please submit pending tasks to qualify.")
else:
print("Attendance check failed. Minimum 75% attendance is required to sit for the exam.")
Output: #
Attendance check passed. Assignment check passed. You are eligible to download your exam admit card.
Example: Nested Pricing and Discount System #
In this scenario, we will calculate the final price of a premium AI course. We will first check if the user is a subscriber, and if they are, we will check their loyalty points to determine their exact discount rate.
base_price = 200
is_subscriber = True
loyalty_years = 3
if is_subscriber:
print("Subscriber status verified.")
if loyalty_years > 2:
discount = 40
print("Loyalty status verified: 40% discount applied.")
else:
discount = 20
print("Standard subscriber: 20% discount applied.")
else:
discount = 0
print("Guest user: No discount applied.")
final_price = base_price - (base_price * discount / 100)
print(f"Original Course Fee: ${base_price}")
print(f"Your Total Balance Due: ${final_price}")
Output: #
Subscriber status verified. Loyalty status verified: 40% discount applied. Original Course Fee: $200 Your Total Balance Due: $120.0
Example: Complex Security Authentication Layer #
In advanced applications, nesting helps validate multi-factor authentication steps. We check if the user entered the correct password, and only then do we check if they entered the correct security PIN.
saved_password = "secure_password123"
saved_pin = "4321"
entered_password = "secure_password123"
entered_pin = "4321"
if entered_password == saved_password:
print("Phase 1 Complete: Password verified.")
if entered_pin == saved_pin:
print("Phase 2 Complete: Security PIN verified.")
print("Access Status: System login successful.")
else:
print("Phase 2 Failed: Incorrect security PIN. Sending alert to registered mobile.")
else:
print("Phase 1 Failed: Incorrect password. Access denied.")
Output: #
Phase 1 Complete: Password verified. Phase 2 Complete: Security PIN verified. Access Status: System login successful.
Challenge Questions for Practice #
Use these challenges to test your understanding of multi-level conditions:
Challenge 1: Nested Age Category and ID Checker #
Set a variable age = 19 and another variable has_id = False. Create a nested conditional block that checks if the age is 18 or above. If it is, print "Age verification passed.". Then, create an inner check to see if has_id is True. If it is, print "Entry allowed.". If it is not, print "Entry denied: ID required.".
Challenge 2: ATM Transaction Guard #
Create a variable pin_correct = True and another variable withdrawal_amount = 300. Set your account_balance = 500. First, check if the PIN is correct. If it is, check if the withdrawal amount is less than or equal to the account balance. If both conditions pass, deduct the amount from the balance and print the remaining balance. If the second condition fails, print "Insufficient funds".
Challenge 3: Job Application Filter #
Create a variable has_degree = True and a variable experience_years = 3. Write a nested if structure that checks if the applicant has a degree. If they do, check if their experience is 2 or more years. If both pass, print "Eligible for interview". If they have a degree but less than 2 years of experience, print "Eligible for junior role". If they don’t have a degree, print "Not eligible".
