Nested if else , nested if and Ternary conditional Statements:

M3-R5.1 · Chapter 4: Operators, Expressions and Python Statements · 5 min read

Nested if else Statements:  A nested if...else statement in Python is when you place one if...else block inside another. It's used to test multiple conditions in a hierarchical or dependent manner.

Syntax :

if condition1:

    if condition2:

        # code block if both condition1 and condition2 are True

    else:

        # code block if condition1 is True but condition2 is False

else:

    # code block if condition1 is False

When to Use:

  • When one condition logically depends on another.
  • To create multi-level decision trees.

Example:

x = 25 or x=int(input(“Plz enter the number”))

if x > 10:

    print("x is greater than 10")

    if x > 20:

        print("x is also greater than 20")

    else:

        print("x is greater than 10 but not more than 20")

else:

    print("x is 10 or less")

Example 1: Write a program to accept price and quantity of item from user and gives the discount as  per the following  slabs Structure:

  • 20% on amount exceeding 10000,
  • 10% for amount between 5000 To10000,
  • 5% if it is between 1000 To 5000.
  • no discount if amount<1000

quan=int(input("Plz enter the quantity of an items"))

price=int(input("Plz enter the price of an items"))

amount = quan*price

print('Amount = ',amount)

if amount > 10000:

   discount = amount * 20 / 100

else:

   if amount > 5000:

      discount = amount * 10 / 100

   else:

      if amount > 1000:

         discount = amount * 5 / 100

      else:

         discount = 0

print ('Payable amount = ', amount - discount)

Nested if statements:  An if statement inside another if.

                                                                                OR

A nested if statement in Python means placing one if statement inside another. It's used when you want to make a decision only after another condition is met.

Example:  Write a Python program that checks if a person is allowed to enter  to vote based on their age and whether they have a valid ID.

age = 20

v_id = True

if age >= 18:

    if v_id:

        print("You are allowed to enter.")

    else:

        print("You need a valid ID to enter.")

else:

    print("You must be at least 18 to enter.")

Example:  Write a Python program to evaluate a student's exam result and attendance status: If the score is 60 or higher, print "You passed the exam. "Then, check if the attendance is 75 or higher:

print "You also met the attendance requirement." If not, print "But you did not meet the attendance requirement." And If the score is less than 60, print "You failed the exam."

score = 85

attendance = 90

if score >= 60:

    print("You passed the exam.")

    if attendance >= 75:

        print("You also met the attendance requirement.")

    else:

        print("But you did not meet the attendance requirement.")

else:

    print("You failed the exam.")

Example:  Write a Python program that takes an integer input and do the following:

  1. Check if the number is positive.
  2. If it is positive, check whether it is even or odd.
  3. If it is even, further check whether it is greater than 10 or not.
  4. Print appropriate messages based on each condition.

num = 15

if num > 0:

    print("Positive number")

    if num % 2 == 0:

        print("Even number")

        if num > 10:

            print("Greater than 10")

        else:

            print("10 or less")

    else:

        print("Odd number")

else:

    print("Not a positive number")

Ternary conditional statement: A ternary conditional statement in Python is a shorthand way of writing an if-else condition in a single line.

Syntax:

          result = Statement1 if condition else Statement 2

Example:

age = 18

status = "Adult" if age >= 18 else "Minor"

print(status)                 # Output: Adult

Example:

x = 8

result = "x is even" if x % 2 == 0 else "x is odd"

print(result)

Example:

a = 5

b = 10

result = "a is smaller" if a < b else "b is smaller or equal"

print(result)