Example HTML page

Nested if-else statement

The elif statements/Nested if-else statements

elif is a keyword in python. It is used as replacement of else if . Whenever you want to add another condition in the same program this elif is used. It is otherwise called as Nested if-else.

The syntax of elif statement is as follows

if (condition): 
      statements 
elif (condition): 
      statements 
else: 
      statements 

Ex 1: A program to check if a number is positive, Negative or Zero.

n=float(input("The number is: "))
if n>0:
    print("Positive number")
elif n==0:
    print("Zero")
else:
    print("Negative number")

Output

The number is: 13

Positive number

Ex 2 A python program that evaluates the bill discount

amount=int(input("Enter amount: "))
if amount<1000:
    discount=amount*0.05
    print ("Discount",discount)
elif amount<5000:
    discount=amount*0.10
    print ("Discount",discount)
else:
    discount=amount*0.10
    print ("Discount",discount)  
print ("Net payable:",amount-discount)

Output

Enter amount: 5460

Discount 546.0

Net payable: 4914.0

Ex 3 Another example program of elif statement.

s=int(input("enter a value"))
if s>10:
   print("C programming")
   print("Java")
   print("Python")
elif s<10 or s>20:
    print("bye")
else:    
    print("good bye")

Output

enter a value 26                            #Testcase 1

C programming

Java

Python

 

enter a value -9                               #Testcase 2

bye

Ex 4 A program to find the given number is greater than the first number or not.

count=0
p=int(input("Starting number is: "))
while count==0:
    q=int(input("Give another number which is greater than starting number: "))
    if q<p:
        print("Oh!Number is not greater")
    elif q==p:
        print("Same number")
    else:
        count=1
print("Yes,the number is greater")

Output

Starting number is: 2                                                                                   #Testcase 1

Give another number which is greater than starting number: 5

Yes,the number is greater

 

 

Starting number is: 5                                                                                     #Testcase 2

Give another number which is greater than starting number: 2

Oh!Number is not greater

Give another number which is greater than starting number: 4

Oh!Number is not greater

Give another number which is greater than starting number: 7

Yes,the number is greater

Ex 5 A program to find the highest number among the three numbers.

n1=float(input("First number: "))
n2=float(input("Second number: "))
n3=float(input("Third number: "))
if(n1>=n2) and (n1>=n3):
    highest=n1
elif(n2>=n1) and (n2>=n3):
    highest=n2
else:
    highest=n3
print("The highest among three is:",highest)

Output

First number: 13                                           #Testcase 1

Second number: 52

Third number: 49

The highest among three is: 52.0

 

First number: 13.54                                   #Testcase 2

Second number: 13.458

Third number: 13.7642

The highest among three is: 13.7642

 

 

Example HTML page

Leave a Reply

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

Pin It on Pinterest