Professional Documents
Culture Documents
Python Lab Programs
Python Lab Programs
1. Write a menu driven program to convert the given temperature from Fahrenheit to
Celsius and vice versa depending upon user‟s choice.
def fahrenheit_to_celsius(temperature):
def celsius_to_fahrenheit(temperature):
while True:
print("Temperature Converter")
print("3. Quit")
if choice == 1:
result = fahrenheit_to_celsius(temperature)
elif choice == 2:
result = celsius_to_fahrenheit(temperature)
elif choice == 3:
break
else:
def square_area(side):
return side * side
def circle_area(radius):
return 3.14 * radius * radius
while True:
print("Area Calculator")
print("1. Rectangle")
print("2. Square")
print("3. Circle")
print("4. Triangle")
print("5. Quit")
choice = int(input("Enter your choice: "))
if choice == 1:
length = float(input("Enter length: "))
width = float(input("Enter width: "))
result = rectangle_area(length, width)
print("Area of rectangle: {:.2f}".format(result))
elif choice == 2:
side = float(input("Enter side: "))
result = square_area(side)
print("Area of square: {:.2f}".format(result))
elif choice == 3:
radius = float(input("Enter radius: "))
result = circle_area(radius)
print("Area of circle: {:.2f}".format(result))
elif choice == 4:
base = float(input("Enter base: "))
height = float(input("Enter height: "))
result = triangle_area(base, height)
print("Area of triangle: {:.2f}".format(result))
elif choice == 5:
break
else:
print("Invalid choice. Try again.")
3. Write a program (WAP) to display the first n terms of Fibonacci series.
def fibonacci(n):
if n <= 0:
return []
elif n == 1:
return [0]
elif n == 2:
return [0, 1]
else:
fib = [0, 1]
fib.append(fib[i-1] + fib[i-2])
return fib
result = fibonacci(n)
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n-1)
result = factorial(n)
5. WAP to find sum of the following series for n terms: 1 – 2/2! + 3/3! ---------------- n/n!
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n-1)
def series_sum(n):
sum = 0
sum += i / factorial(i)
return sum
result = series_sum(n)
result = []
for i in range(len(A)):
row = []
for j in range(len(A[0])):
row.append(A[i][j] + B[i][j])
result.append(row)
return result
result = []
for i in range(len(A)):
row = []
for j in range(len(B[0])):
sum = 0
for k in range(len(A[0])):
row.append(sum)
result.append(row)
return result
print("First matrix:")
for i in A:
print(*i)
print("Second matrix:")
for i in B:
print(*i)
print("Sum of matrices:")
print(*i)
print("Product of matrices:")
print(*i)
# Initialize a string
s = "Hello, World!"
print("Split:", s.split(","))
# Replace a substring
# Format a string
name = "John"
age = 30