Python if, If-Else, Elif Statements – Example Programs

SAMPLE PROGRAMS USING IF, IF ELSE, ELIF, IF-ELSE LADDER IN PYTHON:

(1) Program For Checking Whether a Year is Leap year or not in Python

# ************** PROGRAM 1 *************************************************
#PROGRAM 1: check whether given year is leap or not

'''
y = int(input("Enter the Year:"))
rem = y%4
if rem>0:
print(str(y)+" is not a Leap Year :(.")
else:
print("Yes "+str(y) +" is a Leap year")

'''

(2) Program For Checking whether number is divisible by 3 and 5 in Python Using Elif


## ************** PROGRAM 2 **********************************
#PROGRAM 2: check whether number is divisible by 3 and 5

'''
num = int(input("Enter the Number:"))
rem1 = num%3
rem2 = num%5
if rem1>0:
print ("number is not divisible by 3")
elif rem2>0:
print (" Number is Not divisible by 5")
else:
print("number is divisible by both")
'''

(3) Program For Printing Student Marks, FAIL or PASS and Percentage Details in Python Using If, Else, Elif, Nested If-Esle


#*********************** PROGRAM 3 ***************************************

'''
Read 6 subject marks of a student and check if any one if m1, m2, m3, m4, m5 
and m6 is less than 35. Display pass or Fail, Print total, avg, grades,
avg>=70 is distinction, avg >=60 and 70 first, >=50 and 60 second, else third 
'''


'''

s1 = int(input("Enter marks in subject 1 : "))
s2 = int(input("Enter marks in subject 2 : "))
s3 = int(input("Enter marks in subject 3 : "))
s4 = int(input("Enter marks in subject 4 : "))
s5 = int(input("Enter marks in subject 5 : "))
s6 = int(input("Enter marks in subject 6 : "))

if s1<35 or s2<35 or s3<35 or s4<35 or s5<35 or s6<35:
    print("student failed")
else:
    total = s1+s2+s3+s4+s5+s6
    avg = total/6
    print("Total Marks : ", total)
    print("Percentage : ", avg)
if avg>=70:
    print ("Passed in Distinction")
elif avg<70 and avg>=60:
    print("Passed in First Class")
elif avg<60 and avg>=50:
    print("Passed in Second Class")
else:
    print("Passed in Third Class")

'''

Add a Comment

Your email address will not be published. Required fields are marked *