Python Nested If

Python Nested If Statement means to place one If inside another If Statement. Python If Else statement allows us to print different statements depending upon the expression result (TRUE, FALSE). Sometimes we have to check further even when the condition is TRUE. In these situations, we can use the Python Nested IF statements, but be careful while using it.

In the Python nested if statement example, every person is eligible to work if he is 18 years old or above. Else he is not qualified. However, companies won’t offer a job to every person. So, we use another If condition called Python Nested If Statement to check his education qualifications or any specific company requirements.

Python Nested If Statement Syntax

The Syntax of the Python Nested If Statement is shown below.

if ( test condition 1):
    # If test condition 1 is TRUE, then it checks for test condition 2
    if ( test condition 2):
         # If test condition 2 is TRUE, then these true lines executed
         Test condition 2 True statements
    else:
         # If test condition 2 is FALSE, then these false lines executed
         Test condition 2 False statements
else:
    # If test condition 1 is FALSE, then these lines executed
    Test condition 1 False lines

Once we hit enter after a semicolon, it starts the next line with Tab space, and this Tab space acts as curly braces ({ }) in other programming languages.

If the test condition one present in the above structure is true, then it goes to Python nested if statement.

  • If test condition 2 is True, the Test Condition 2 True statements are executed.
  • Else (it means test condition 2 is false) Test Condition 2 false code executed.

If test condition 1 is false, the Test Condition 2 false statements are executed. By clicking backspace, we can exit from the If Else block.

Python Nested if Flow Chart

The following flow chart will explain your Python Nested If Statement perfectly.

FLOW CHART For Python Nested If Statement

If Test Condition1 is FALSE, then STATEMENT3 executes. If Test Condition1 is TRUE, it checks for Test Condition2, and if it is TRUE, then STATEMENT1 executes else STATEMENT2.

Python Nested If Example

In this Python Nested If Program, the User can enter his age, and we are going to store it in the variable age. If the age is less than 18, we are going to print two lines. When the condition fails, we check one more condition (Nested), and if it succeeds, we print something. If the nested condition fails, we print some other code using the nested if statement. To demonstrate this one, Please add the following script in a new file.

age = int(input(" Please Enter Your Age Here:  "))
if age < 18:
    print(" You are Minor ") 
    print(" You are not Eligible to Work ") 
else:
    if age >= 18 and age <= 60:
        print(" You are Eligible to Work ")
        print(" Please fill in your details and apply")
    else:
        print(" You are too old to work as per the Government rules")
        print(" Please Collect your pension!")

Please save the Python Nested If file and run the script by selecting the Run Module or clicking F5. Once you click on Run Module, Our Shell will pop up with the message “Please Enter Your Age Here:” .

OUTPUT 1: Here, we entered the age of 14. The first If condition is TRUE, so the output displays the print function inside the If block.

Please Enter Your Age Here: 14
You are Minor
You are not Eligible to Work

Here, we entered the age = 27. The first If condition is FALSE. So, it will enter into the else block, and there it checks for another condition. Here, the Python Nested If statement is TRUE, so the output displays the print function inside it.

Please Enter Your Age Here: 27
You are Eligible to Work
Please fill in your details and apply

We entered the age = 61. First If condition is FALSE. So, it will enter into the else block, and there it will check for other conditions. Here the Nested If statement is also FALSE, so the output is showing the print functions inside the else block.

Python Nested If Statement 6

In this Python Nested If statement example, First, we declared the age variable. Next, it asks the user to enter any integer value. int() restricts the user not to enter noninteger values

age = int(input(" Please Enter Your Age Here:  "))

If the person’s age is less than 18, then the following code will print.

 print(" You are Minor ") 
 print(" You are not Eligible to Work ")

If the person’s age is greater than or equal to 18, the first condition fails, so it checks the else statement. In the Else statement, there is another if condition (called Nested If).

Python Nested IF Statement will check whether the person’s age is greater than or equal to 18 and less than or equal to 60. When the condition is TRUE, then the following code will print

 print(" You are Eligible to Work ")
 print(" Please fill in your details and apply")

When the condition inside the Nested If is FALSE. Then it will check the else block and print the following lines of code. Please refer to the If Else and If condition articles.

 print(" You are too old to work as per the Government rules")
 print(" Please Collect your pension!")