About Lesson
elif
Statement
elif
(else if) allows for multiple conditions to be checked sequentially. If the if
condition fails, Python checks the elif
condition.
Program1: Find biggest between 2 nos using elif statement
Python
x = 7
if x > 10:
print("x is greater than 10")
elif x > 5:
print("x is greater than 5 but less than or equal to 10")
else:
print("x is 5 or less")
Program2: Program to enter a day of week and print name of day
Python
#enter a day of week and print name of the day
#if elif else
day=int(input("enter day of the week"))
if day==1:
print("Today is Monday")
elif day==2:
print("Today is Tue")
elif day==3:
print("Today is Wed")
elif day==4:
print("Today is Thu")
elif day==5:
print("Today is Fri")
elif day==6:
print("Today is Sat")
elif day==7:
print("Today is Sun")
else:
print("Invalid Input")
Program 3: Find biggest between 2 nos using ternery operator
Python
#biggest between 2 nos using ternery operator
#syntax expr1 if condition else expr2
a=int(input("enter 1st number"))
b=int(input("enter 2nd number"))
c=a if a>b else b # in C a>b?a:b;
print("Biggest Number is ",c)
print("A is Biggest") if a>b else print("B is Biggest")
# in C a>b?printf("A is Big"):print("B is Big")