Python Programming – If-Elif-Else Statements

You can learn about Control Statements in Python Programs with Outputs helped you to understand the language better.

Python Programming – If-Elif-Else Statements

In the if statement, the statement following the condition or the keyword else is a single executable statement, such as an assignment statement. If multiple tasks are to be performed depending on a condition, then they can be grouped together within the block to form a single compound statement. Thus, you can use more than one statement by embedding the statements in a block. In the compound statement, there is no limit to the number of statements that can appear inside the body, but there has to be at least one. Indentation level is used to tell Python which statement(s) belongs to which block.

If you want to check for several conditions, you can use elif, which is short for “else if”. It is a combination of an if clause and an else clause. Like the else, the elif statement is optional. However, unlike else for which there can be at the most one statement, there can be unlimited number of elif statements following an if. The syntax of the if…elif…else statement is as follows:
if(expression1):
statement(s)
elif(expression2):
statement(s)
elif(expression3):
statement(s).
else:
statement(s)
For example,
grade = eval ( input ( ‘ Enter your score : ‘ ) )
if grade>=90:
print(‘A’)
elif (grade>=70):
print(‘B’)
elif (grade>=60):
print(‘C’)
elif (grade>=40):
print(‘D’)
else:
print(‘F’)

  • In Python, like the else, the elif statement is optional.

Simple Programs

Example 12.
Program to print the entered number is positive, negative or zero using if…elif…else statement.

# Program to print number is positive, negative or zero
num = int ( input ( ‘ Enter a number: ‘ ) )
if ( num > 0 ):
print ( ‘ The number is positive ‘ )
elif ( num < 0 ):
print ( ‘ The number is negative ‘ )
else:
print ( ‘ The number is zero ‘ )RUN
>>>
Enter a number: 7
The number is positive
>>>
Enter a number: -5
The number is negative
>>>
Enter a number: 0
The number is zero
>>>

Example 13.
Program to Calculate Electricity Bill.

# Program to Calculate Electricity Bill
units = int ( input ( ” Enter Number of Units Consumed : ” ) )
if ( units > 500 ):
amount = units * 9.25
surcharge = 80
elif ( units >= 300 ):
amount = units * 7.75
surcharge = 70
elif ( units >= 200 ):
amount = units * 5.25
surcharge = 50
elif ( units >= 100 ):
amount = units * 3.75
surcharge = 30
else:
amount = units * 2.25
surcharge = 20
billtotal = amount + surcharge
print(“Electricity Bill = %.2f” %billtotal )RUN
Enter Number of Units Consumed : 450
Electricity Bill = 3557.50

Example 14.
Program to calculate tax.

# Program to calculate tax
name=input ( ” Enter the name of Employee :11 )
salary = eval ( input ( ‘ Enter salary: ‘ ) )
if salary <=500000:
taxamt=0.05*salary
elif salary <=600000:
taxamt=0.07* salary
elif salary <=700000:
taxamt =0.08*salary
else:
taxamt=0.10*salary
print ( ” Name: “, name, “Salary :”, salary, “Tax :”, taxamt )RUN
Enter the name of Employee :Aman
Enter salary: 770000
Name: Aman Salary : 770000 Tax : 77000.0

Example 15.
Program to print numbers in ascending order.

# Program to print numbers in ascending order
num1 = int ( input ( ” Enter first number: ” ) )
num2 = int ( input ( ” Enter second number: ” ) )
num3 = int ( input ( ” Enter third number: ” ) )
if num1 < num2 and num1 < num3:
if num2 < num3:
x, y, z = num1, num2, num3
else:
x, y, z = num1, num3, num2
elif num2 < num1 and num2 < num3:
if numl < num3:
x, y, z = num2, num1, num3
else:
x, y, z = num2, num3, num1
else:
if num1<num2:
x, y, z = mm3, num1, mm2
else:
x, y, z = num3, mm2, num1print(“Numbers in ascending order are: “, x,y,z)RUN
>>>
Enter first number: 27
Enter second number: 25
Enter third number: 45
Numbers in ascending order are: 25 27 45
>>>

Let’s Try

Write a program to enter three numbers and print numbers in descending order.

Leave a Reply

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