Python-if-elif-statement .

In this session i have discussed if-elif in Python .


IF-ElIF :

It is also decision making and branching statement . If you have multiple choice then you can use the if-elif . In if-elif case first control check the if condition if it is true then the contorl come inside the if body other wise control transfer to elif statement check the condition if it is true then contorl come inside the elif body and go on . If no any condition match then last else statement become execute .

IF-ElIF-statement-syntax :

 image


Example : Write a python program that check the days of week according to given value .

i=int(input("Enter the number of days"))
if(i==1):
    print("Sunday")
elif(i==2):
    print("Monday")
elif(i==3):
    print("Tuesday")
elif(i==4):
    print("Wednesday")
elif(i==5):
    print("Thursday")
elif(i==6)
    print("Friday")
elif(i==7):
    print("Sutarday")
else:
    print("Invalid Number")
    
    OUTPUT : Enter the number of days 3
             Tuesday
    


Example : Write a program that perform 1-Addition , 2-Subtraction , 3-multiplication , 4 Division .

print("1-Addition")
print("2-Subtraction")
print("3-Multiplication")
print("4-Division")
op=int(input("Enter the choice"))
x=int(input("Enter the First number"))
y=int(input("Enter the Second number"))
if(op==1):
    sum=x+y;
    print("Sum : %d"%(sum))
elif(op==2):
    sub=x-y;
    print("Sub : %d"%(sub))
elif(op==3):
    mul=x*y;
    print("Mul : %d"%(mul))
elif(op==4):
    div=x/y
    print("Div : %d"%(div))
else:
    print("Invalid Number");
    
    OUTPUT :    1-Addition
                2-Subtraction
                3-Multiplication
                4-Division
                Enter the choice 2
                Enter the first number 20
                Enter the second number10
                Sub : 10
    


Welcome to AndroidATPoint.

back image                     next image