Example HTML page

if-elif-else statement

The if-elif-else statement

When an if condition is true then the statements in if section are executed. When the condition is evaluated to false the statements in the else section will be executed. In the meanwhile it also evaluates the elif sections. If it results to true then it moves to next immediate elif section otherwise the execution flow transfers to the else section.

The syntax of  if-elif-else statements is shown in below

if boolean-expression:
   statements
elif boolean-expression:
   statements
elif boolean-expression:
   statements
elif boolean-expression:
   statements
else:
   statements

Ex 1: A sample python program on if-elif-else statement.

Totalmarks = int(input(" Enter Your Total Marks: "))
if Totalmarks >= 540:
    print(" You are eligible for Full Scholarship ")
elif Totalmarks >= 480:
    print(" You are eligible for 50 Percent Scholarship ")
elif Totalmarks >= 400:
    print(" You are eligible for 10 Percent Scholarship ")
else:
    print(" You are Not eligible for Scholarship ")

Output

Enter Your Total Marks: 495

You are eligible for 50 Percent Scholarship

Ex 2: A program to find the type of the variable.

a = 1+2j
if (type(a) == int):
    print("Type of the variable is Integer")
elif (type(a) == float):
    print("Type of the variable is Float")
elif (type(a) == complex):
    print("Type of the variable is Complex")
elif (type(a) == bool):
    print("Type of the variable is Bool")
elif (type(a) == str):
     print("Type of the variable is String")
else:
    print("Type of the variable is Unknown")

Output

Type of the variable is Complex

Ex 3: A sample program to print the grade.

num=int(input("Marks: "))
if num>89:
    letter="A"
elif num>79:
    letter="B"
elif num>69:
    letter="C"
else:
    letter="D"
print("The Grade is",letter)

Output

Marks: 75

The Grade is C

Ex 4 Another example program of if-elif-else statement.

b=int(input("Enter an integer: "))
if b<0:
    print("Sudha")
elif b==0:
    print("Hello sudhakar")
elif b==1:
    print("How are you?")
else:
    print("Bye")

Output

Enter an integer: 5                                       #Test case 1

Bye

 

Enter an integer: -5                                      #Test case2

Sudha

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

k=int(input("Give a number: "))
if(k>100):
    print("Greater than 100")
elif(k>70):
    print("Greater than 70")
elif(k>30):
    print("Greater than 30")
else:
    print("End")

Output

Give a number: 89

Greater than 70

Ex 6 A program on if-elif-else statement using range( ) function.

for x in range(15):
    if x % 3 == 0 and x % 5 == 0:
        print('MallaSudhakar')
    elif x % 3 == 0:
        print('Malla')
    elif x % 5 == 0:
        print('Sudhakar')
    else:
        print('*')

Output

MallaSudhakar

*

*

Malla

*

Sudhakar

Malla

*

*

Malla

Sudhakar

*

Malla

*

*

Explanation

In the above code the range is ‘15’. So it evaluates until 14. Print ‘Malla’ for every number that’s divisible by 3, print ‘Sudhakar’ for every number divisible by 5, and print ‘MallaSudhakar’ for every number divisible by 3 and by 5. If the number is not divisible either by 3 or 5, then print a dash (‘*‘).

Example HTML page

Leave a Reply

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

Pin It on Pinterest