Python Programs Demonstrating
Arithmetic Operators
Arithmetic Operators - Example Programs
These programs demonstrate the use of arithmetic operators like + , - , * , / ,
// , % , **
1. WAP in Python to calculate Simple Interest where
principal is Rs. 1200, rate is 8% [Link] time is 2 years
In [4]: # create required variables and assign value
prin = 1200
rate = 8
time = 2
# create variable SI to hold answer and perform calculations
SI = (prin * rate * time) / 100
# display the result using print function
print("The Simple Interest is:", SI)
The Simple Interest is: 192.0
2. WAP in Python to calucalte Area of a Triangle were base
is 10 cm and height is 5 cm.
In [6]: # create required variables and assign value
base = 10
height = 5
# create variable to hold answer and perform calculations
area = (base * height) / 2
# display the result using print function
print("Area of Triangle:", area)
Area of Triangle: 25.0
3. WAP in Python to convert temperature from Celsius to
Fahrenheit. Assume the temperature to be 30°C.
In [8]: # create required variables and assign value
celsius = 30
# create variable to hold answer and perform calculations
fahrenheit = (celsius * 9 / 5) + 32
# display the result using print function
print("Temperature in Fahrenheit is:", fahrenheit)
Temperature in Fahrenheit is: 86.0
4. WAP in Python to calculate the Average of any 3
Numbers.
In [10]: #create required variables and assign value
num1 = 15
num2 = 25
num3 = 35
#create variable to hold answer and perform calculations
average = (num1 + num2 + num3) / 3
#display the result
print("The Average is:", average)
The Average is: 25.0
5. WAP in Python to find Square and Cube of any given
number
In [12]: #create required variables and assign value
number = 4
#create variables to hold answers and perform calculations
square = number ** 2 #number to the power of 2
cube = number ** 3 #number to the power of 3
#display the result
print("Square of given number is:", square)
print("Cube of given number is:", cube)
Square of given number is: 16
Cube of given number is: 64
6. WAP in Python to display the remainder when Dividing
Two Numbers
In [14]: #variable to hold dividend
dividend = 29
#variable to hold divisor
divisor = 4
#variable to hold answers and perform calculations
remainder = dividend % divisor
#display the result
print("While dividing", dividend, "by", divisor, "the Remainder is", remainder)
While dividing 29 by 4 the Remainder is 1
7. Write a Python program to calculate the average marks
obtained per subject using floor division. Input the marks
of 5 individual subjects, compute the total and then
calculate the approximate marks per subject using floor
division.
In [16]: # Create required varaibles to store Marks of individual subject
subject1 = 56
subject2 = 48
subject3 = 59
subject4 = 52
subject5 = 60
# Calculate total marks and store it in a variable
totalMarks = subject1 + subject2 + subject3 + subject4 + subject5
# Number of subjects
subjects = 5
# Calculate average marks per subject using floor division
marksPerSubject = totalMarks // subjects
# Display the result using print function
print("Each subject got approximately:", marksPerSubject)
Each subject got approximately: 55