Practical – 2
Note : Write the theory on types of operators in python with all the charts.
Then write the given below programs with output
1. Write a program to simulate a simple calculator (+ - / * %) that takes two operands as input and displays
the result.
2. Write a program to find area and perimeter of geometric objects.
3. The distance between two cities (in km.) is input through the keyboard. Write a program to convert and
print this distance in meters, feet, inches and centimeters.
4. Write a Program to interchange two numbers.
5. Write a program to compute Fahrenheit from centigrade
# 1 . Program to demonstrate a calculator
print("Demo of a simple Calculator\n");
print("Please select operation -\n" \
"1. Add\n" \
"2. Subtract\n" \
"3. Multiply\n" \
"4. Divide\n")
# User input
val = int(input("Select operations form 1, 2, 3, 4 :"))
x= int(input("Enter first number: "))
y = int(input("Enter second number: "))
if val == 1:
print(x, " + ",y, " = ",x+y)
elif val == 2:
print(x, " - ",y, " = ",x-y)
elif val == 3:
print(x, " * ",y, " = ",x*y)
elif val == 4:
print(x, " / ",y, " = ",x/y)
else:
print("Invalid input")
OUTPUT -1
Demo of a simple Calculator
Please select operation -
1. Add
2. Subtract
3. Multiply
4. Divide
Select operations form 1, 2, 3, 4 :2
Enter first number: 6
Enter second number: 4
6 - 4 = 2
____________________________________________________________________________
OUTPUT -1
Demo of a simple Calculator
Please select operation -
1. Add
2. Subtract
3. Multiply
4. Divide
Select operations form 1, 2, 3, 4 :4
Enter first number: 7
Enter second number: 5
7 / 5 = 1.4
# 2 . Program to find area and perimeter of geometric objects.
shape=input("Enter the name of shape : ")
if shape == "rectangle":
l = int(input("Enter the length: "))
b = int(input("Enter the breadth: "))
print("The area of rectangle = ",l*b)
print("The perimeter of rectangle : ",2*(l+b))
elif shape == "square":
s = int(input("Enter the length of side : "))
print("The area of square is : ",s*s)
print("The perimeter of the square : ",4*s)
elif shape == "triangle":
h = int(input("Enter the height : "))
b = int(input("Enter the breadth : "))
fs=int(input("Enter the first side of tri : "))
ss=int(input("Enter the second side of the triangle : "))
print("Area of the triangle : ", 0.5*b*h)
print("Perimeter of triangle : ",b+fs+ss)
elif shape == "circle":
r = int(input("Enter circle's radius length: "))
pi = 3.14
print("The area of circle : ",pi*r*r)
print("Perimeter of circle : " , 2*3.14*r)
else:
print("Sorry! This shape is not available")
OUTPUT
Enter the name of shape : rectangle
Enter the length: 12
Enter the breadth: 10
The area of rectangle = 120
The perimeter of rectangle : 44
OUTPUT
Enter the name of shape : square
Enter the length of side : 5
The area of square is : 25
The perimeter of the square : 20
“”” 3 .The distance between two cities (in km.) is input through the keyboard. Write a
program to convert and print this distance in meters, feet, inches and centimeters. “””
d=int(input("Enter distance in KMs : "))
m=d*1000
print("Distance in meters : ", m)
f=m*3.28084
print("Distance in feet : ",f)
i=f*12
print("Distance in inches : ",i)
cm=i*2.54
print("Distance in cm : ",cm)
OUTPUT
Enter distance in KMs : 1
Distance in meters : 1000
Distance in feet : 3280.84
Distance in inches : 39370.08
Distance in cm : 100000.0032
# 4 . Write a Program to interchange two numbers.
x=int(input("Enter any no. : "))
y=int(input("Enter any no. : "))
print("Before swapping : \n x = {} \ty = {}".format(x,y))
t=x
x=y
y=t
print("After swapping : \n x = {} \ty = {}".format(x,y))
OUTPUT
Enter any no. : 10
Enter any no. : 20
Before swapping :
x = 10 y = 20
After swapping :
x = 20 y = 10
# 5 .Write a program to compute Fahrenheit from centigrade
c=float(input("Enter temperature in celcius : "))
f=(c*1.8)+32
print("Temperature in fahrenheit : ",f)
OUTPUT
Enter temperature in celcius : 32
Temperature in fahrenheit : 89.6