Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Python Lab

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):

return (temperature - 32) * 5/9

def celsius_to_fahrenheit(temperature):

return temperature * 9/5 + 32

while True:

print("Temperature Converter")

print("1. Fahrenheit to Celsius")

print("2. Celsius to Fahrenheit")

print("3. Quit")

choice = int(input("Enter your choice: "))

if choice == 1:

temperature = float(input("Enter temperature in Fahrenheit: "))

result = fahrenheit_to_celsius(temperature)

print("Temperature in Celsius: {:.2f}".format(result))

elif choice == 2:

temperature = float(input("Enter temperature in Celsius: "))

result = celsius_to_fahrenheit(temperature)

print("Temperature in Fahrenheit: {:.2f}".format(result))

elif choice == 3:

break

else:

print("Invalid choice. Try again.")


2. Write a menu-driven program, using user-defined functions to find the area of rectangle,
square, circle and triangle by accepting suitable input parameters from user

def rectangle_area(length, width):


return length * width

def square_area(side):
return side * side

def circle_area(radius):
return 3.14 * radius * radius

def triangle_area(base, height):


return 0.5 * base * height

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]

for i in range(2, n):

fib.append(fib[i-1] + fib[i-2])

return fib

n = int(input("Enter number of terms: "))

result = fibonacci(n)

print("The first {} terms of the Fibonacci series are: {}".format(n, result))


4. WAP to find factorial of the given number.

def factorial(n):

if n == 0 or n == 1:

return 1

else:

return n * factorial(n-1)

n = int(input("Enter a number: "))

result = factorial(n)

print("The factorial of {} is {}".format(n, result))

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

for i in range(1, n+1):

sum += i / factorial(i)

return sum

n = int(input("Enter number of terms: "))

result = series_sum(n)

print("The sum of the series for {} terms is {:.2f}".format(n, res


6. WAP to calculate the sum and product of two compatible matrices.

def add_matrices(A, B):

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

def multiply_matrices(A, B):

result = []

for i in range(len(A)):

row = []

for j in range(len(B[0])):

sum = 0

for k in range(len(A[0])):

sum += A[i][k] * B[k][j]

row.append(sum)

result.append(row)

return result

A = [[1, 2], [3, 4]]

B = [[5, 6], [7, 8]]

print("First matrix:")

for i in A:

print(*i)
print("Second matrix:")

for i in B:

print(*i)

print("Sum of matrices:")

for i in add_matrices(A, B):

print(*i)

print("Product of matrices:")

for i in multiply_matrices(A, B):

print(*i)

7. WAP to explore String functions

# Initialize a string

s = "Hello, World!"

# Length of the string

print("Length of the string:", len(s))

# Convert string to upper case

print("Upper case:", s.upper())

# Convert string to lower case

print("Lower case:", s.lower())

# Split string into a list

print("Split:", s.split(","))

# Replace a substring

print("Replace:", s.replace("Hello", "Hi"))


# Check if string starts with a specific substring

print("Starts with:", s.startswith("Hello"))

# Check if string ends with a specific substring

print("Ends with:", s.endswith("World!"))

# Find the index of a substring

print("Index of 'World':", s.index("World"))

# Check if string contains a specific substring

print("Contains 'World':", "World" in s)

# Concatenate two strings

print("Concatenation:", s + " How are you?")

# Format a string

name = "John"

age = 30

print("My name is {} and I am {} years old.".format(name, age))

You might also like