Python – Nested If

Nested-If:  Writing if block inside another if block

Check the number is even or not only if the Number is positive

n = int(input(“Enter number :”))
if n>=0:
    if n%2==0:
        print(“Even number”)
    else:
        print(“Not even number”)
else:
    print(“Negative”)

Check the biggest of 2 numbers only if the 2 numbers are not equal:

print(“Enter 2 integers :”)
a = int(input())
b = int(input())
if(a!=b):
    if(a>b):
        print(“a is big”)
    else:
        print(“b is big”)
else:
    print(“equal numbers given”)

Display Student Grade only if the Student passed in all subjects:

print(“Enter 3 subject marks :”)
m1 = int(input())
m2 = int(input())
m3 = int(input())
 
if(m1>=40 and m2>=40 and m3>=40):
    avg = (m1+m2+m3)/3
    if(avg>=75):
        print(“Distinction”)
    elif(avg>=60):
        print(“A-Grade”)
    elif(avg>=50):
        print(“B-Grade”)
    else:
        print(“C-Graade”)
else:
    print(“Fail”)
Scroll to Top