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, also called Python Nested If Statement, to check his education qualifications or any specific company requirements.

Python Nested If Statement Syntax

The Python Nested If Statement Syntax is

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 the 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 the 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 the test condition 1 is false, the Test Condition 2 false statements executed. By clicking backspace, we can exit from the If Else block.

Nested if Flow Chart

The following flow chart will explain you Python Nested If Statement perfectly

FLOW CHART For Python Nested If Statement

If the Test Condition1 is FALSE, then STATEMENT3 executed. If Test Condition1 is TRUE, it checks for the Test Condition2, and if it is TRUE, then STATEMENT1 executed 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 python nested if statement. To demonstrate the Python nested if, Please add the following script in a new file.

# Example for Nested If

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 click F5. Once you click on Run Module, Our Python 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 is displaying 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. 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 is displaying 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 condition. Here Python 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 age variable. Next, it asks the user to enter any integer value. int() restrict 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 printed.

 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 as 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 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 If Else and If condition articles.

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