Python Nested If

Python Nested If Statement means to place one If Statement 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.

The Python nested if statement example, every person is eligible to work if he is 18 years old or above else he is not eligible. However, companies won’t offer a job to every person. So, we use another If condition, also called as Nested If Statement in Python 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 statements executed
         Test condition 2 True statements
    else:
         # If test condition 2 is FALSE, then these false statements executed
         Test condition 2 False statements
else:
    # If test condition 1 is FALSE, then these statements executed
    Test condition 1 False statements

Once we hit the enter after a semicolon, python starts the next statement 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 executed.
  • Else (it means test condition 2 is false) Test Condition 2 false statements 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 statement block.

Flow Chart for Python Nested if Statement

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, 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 statements. 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 statement using python nested if statement. To demonstrate the Python nested if, Please add the following script in a new file.

# Example for Python Nested If Statement

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 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. First If condition is TRUE so, the output is displaying the print statements inside the If statement.

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 else block, and there it checks for the Nested If condition. Here, the Python Nested If statement is TRUE so, the output is displaying the print statements inside the Nested If statement.

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 else block, and there it will check for the Nested IF condition. Here Python Nested If statement is also FALSE so, the output is showing the print statements inside the else block of Nested If statement.

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 statements 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 statements will print

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

When the condition inside the Nested If Statement is FALSE. Then it will check the else statement and print the following statements. Please refer to Python If Else and Python If Statement articles.

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