Programming in Python Laboratory
UGCA1917
Machine Exercise : 1
1. Compute sum, subtraction, multiplication, division and exponent of given
variables input by the user.
a=int(input("enter first number"))
b=int(input("second number"))
add = a+b
sub =a-b
mult = a*b
div = a/b
exp = a**b
print("\n sum is :", add, "\n difference is :", sub, "\n product is:",mult, "\n quoitent is :",div, "\n
exponent is :",exp)
Output:
Enter first number 3
second number 2
sum is 5
difference is 1
product is 6
quotient is 1.5
exponent is 9
2. Compute area of following shapes: circle, rectangle, triangle, square,
trapezoid and parallelogram.
import math
# Circle
radius = float(input("Enter radius of circle: "))
area_circle = [Link] * radius * radius
print("Area of Circle:", area_circle)
# Rectangle
length = float(input("Enter length of rectangle: "))
width = float(input("Enter width of rectangle: "))
area_rectangle = length * width
print("Area of Rectangle:", area_rectangle)
# Triangle
base = float(input("Enter base of triangle: "))
height = float(input("Enter height of triangle: "))
area_triangle = 0.5 * base * height
print("Area of Triangle:", area_triangle)
# Square
side = float(input("Enter side of square: "))
area_square = side * side
print("Area of Square:", area_square)
# Trapezoid
a = float(input("Enter first parallel side of trapezoid: "))
b = float(input("Enter second parallel side of trapezoid: "))
height = float(input("Enter height of trapezoid: "))
area_trapezoid = 0.5 * (a + b) * height
print("Area of Trapezoid:", area_trapezoid)
# Parallelogram
base = float(input("Enter base of parallelogram: "))
height = float(input("Enter height of parallelogram: "))
area_parallelogram = base * height
print("Area of Parallelogram:", area_parallelogram)
Output:
Enter radius of circle: 7
Area of Circle: 153.93804002589985
Enter length of rectangle: 10
Enter width of rectangle: 5
Area of Rectangle: 50
Enter base of triangle: 8
Enter height of triangle: 6
Area of Triangle: 24.0
Enter side of square: 4
Area of Square: 16
Enter first parallel side of trapezoid: 6
Enter second parallel side of trapezoid: 4
Enter height of trapezoid: 5
Area of Trapezoid: 25.0
Enter base of parallelogram: 9
Enter height of parallelogram: 4
Area of Parallelogram: 36
3. Compute volume of following 3D shapes: cube, cylinder, cone and sphere.
a = int(input(“enter the side of cube:”)
print (“volume of cube is :”a*a*a)
r = int (input (“enter the radius”)
h=int(input(“enter the height”)
print (“volume of cylinder is “,3.14*r*r*h)
r = int (input (“enter the radius”)
h=int(input(“enter the height”)
print (“volume of cone is “,3.14*r*r*h/3)
r = int (input (“enter the radius”)
print(“ volume of sphere is “4/3*r*r*r*3.14)
Output:
enter the side of cube: 4
volume of cube is :64
enter the radius 4
enter the height 8
volume of cylinder is 402.12
enter the radius 2
enter the height 5
volume of cone is 20.94
enter the radius 5
volume of sphere is 523.6
4. Compute and print roots of quadratic equation ax2+bx+c=0, where the
values of a, b, and c are input by the user.
import cmath
a=1
b=5
c=6
# calculate the discriminant
d = (b**2) - (4*a*c)
# find two solutions
sol1 = (-[Link](d))/(2*a)
sol2 = (-b+[Link](d))/(2*a)
print('The solution are {0} and {1}'.format(sol1,sol2))
Output:
Enter a: 1
Enter b: 5
Enter c: 6
The solutions are (-3+0j) and (-2+0j)