Lab Work - Python Programs
Index
1. Program 1: Arithmetic Operations ............................ 2
2. Program 2: Area of Shapes ............................ 3
3. Program 3: Volume of 3D Shapes ............................ 4
4. Program 4: Roots of Quadratic Equation ............................ 5
5. Program 5: Numbers Not Divisible by 3, 6, or 9 ............................ 6
Page 1
Lab Work - Python Programs
Program 1: Arithmetic Operations
# Program to compute sum, subtraction, multiplication, division and exponent
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
print("Sum:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
if b != 0:
print("Division:", a / b)
else:
print("Division: Undefined (division by zero)")
print("Exponent:", a ** b)
Sample Output:
Enter first number: 5
Enter second number: 3
Sum: 8.0
Subtraction: 2.0
Multiplication: 15.0
Division: 1.6666666667
Exponent: 125.0
Page 2
Lab Work - Python Programs
Program 2: Area of Shapes
import math
# Circle
r = float(input("Enter radius of circle: "))
print("Area of Circle:", [Link] * r**2)
# Rectangle
l = float(input("Enter length of rectangle: "))
w = float(input("Enter width of rectangle: "))
print("Area of Rectangle:", l * w)
# Triangle
base = float(input("Enter base of triangle: "))
height = float(input("Enter height of triangle: "))
print("Area of Triangle:", 0.5 * base * height)
# Square
side = float(input("Enter side of square: "))
print("Area of Square:", side ** 2)
# Trapezoid
a = float(input("Enter base1 of trapezoid: "))
b = float(input("Enter base2 of trapezoid: "))
h = float(input("Enter height of trapezoid: "))
print("Area of Trapezoid:", 0.5 * (a + b) * h)
# Parallelogram
base = float(input("Enter base of parallelogram: "))
height = float(input("Enter height of parallelogram: "))
print("Area of Parallelogram:", base * height)
Sample Output:
Sample Input/Output:
Enter radius of circle: 3
Area of Circle: 28.2743
Enter length of rectangle: 5
Enter width of rectangle: 4
Area of Rectangle: 20.0
Enter base of triangle: 6
Enter height of triangle: 4
Area of Triangle: 12.0
Enter side of square: 4
Area of Square: 16.0
Enter base1 of trapezoid: 3
Enter base2 of trapezoid: 5
Enter height of trapezoid: 4
Area of Trapezoid: 16.0
Enter base of parallelogram: 6
Page 3
Lab Work - Python Programs
Enter height of parallelogram: 4
Area of Parallelogram: 24.0
Page 4
Lab Work - Python Programs
Program 3: Volume of 3D Shapes
import math
# Cube
side = float(input("Enter side length of cube: "))
print("Volume of Cube:", side ** 3)
# Cylinder
r = float(input("Enter radius of cylinder: "))
h = float(input("Enter height of cylinder: "))
print("Volume of Cylinder:", [Link] * r**2 * h)
# Cone
r = float(input("Enter radius of cone: "))
h = float(input("Enter height of cone: "))
print("Volume of Cone:", (1/3) * [Link] * r**2 * h)
# Sphere
r = float(input("Enter radius of sphere: "))
print("Volume of Sphere:", (4/3) * [Link] * r**3)
Sample Output:
Sample Input/Output:
Enter side length of cube: 3
Volume of Cube: 27.0
Enter radius of cylinder: 3
Enter height of cylinder: 5
Volume of Cylinder: 141.3717
Enter radius of cone: 3
Enter height of cone: 5
Volume of Cone: 47.1239
Enter radius of sphere: 3
Volume of Sphere: 113.0973
Page 5
Lab Work - Python Programs
Program 4: Roots of Quadratic Equation
import math
a = float(input("Enter value of a: "))
b = float(input("Enter value of b: "))
c = float(input("Enter value of c: "))
d = b**2 - 4*a*c
if d > 0:
root1 = (-b + [Link](d)) / (2*a)
root2 = (-b - [Link](d)) / (2*a)
print("Two Real Roots:", root1, "and", root2)
elif d == 0:
root = -b / (2*a)
print("One Real Root:", root)
else:
real = -b / (2*a)
imag = [Link](-d) / (2*a)
print(f"Complex Roots: {real}+{imag}i and {real}-{imag}i")
Sample Output:
Sample Input/Output:
Enter value of a: 1
Enter value of b: -3
Enter value of c: 2
Two Real Roots: 2.0 and 1.0
Page 6
Lab Work - Python Programs
Program 5: Numbers Not Divisible by 3, 6, or 9
N = int(input("Enter the value of N: "))
for i in range(1, N+1):
if i % 3 != 0 and i % 6 != 0 and i % 9 != 0:
print(i, end=", ")
Sample Output:
Enter the value of N: 20
1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20,
Page 7