Python Ws 1.3 New Sai
Python Ws 1.3 New Sai
WORKSHEET 1.3
1. Aim: Write a function that takes two numbers as arguments and returns their sum,
difference, product, and quotient
2. Program Code:
def arithmetic_operations(num1, num2):
try:
sum_result = num1 + num2
difference_result = num1 - num2
product_result = num1 * num2
if num2 != 0:
quotient_result = num1 / num2
else:
raise ZeroDivisionError("Error: Division by zero")
results = arithmetic_operations(num1,
print("Difference:", results[1])
print("Product:", results[2])
print("Quotient:", results[3])
3. Screenshot of Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
1. Aim: Create a function with default arguments that calculates the area of a rectangle.
2. Program Code:
def calculate_rectangle_area(length=1,width=1)
area = length * width
return area
print("Area with default dimensions:", calculate_rectangle_area())
print("Area with length 10 and width 15:", calculate_rectangle_area(10, 15))
3.Screenshot of Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
1. Aim: Write a lambda function that returns the square of a number, and use it in a
list comprehension to square all numbers in a given list.
2. Program code:
# Define the lambda function
square = lambda x: x ** 2 #
List of numbers numbers =
[1, 2, 3, 4, 5]
# Use the lambda function in a list comprehension to square all numbers
squared_numbers = [square(num) for num in numbers] print("Original
numbers:", numbers) print("Squared numbers:", squared_numbers)
3. Screenshot of Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
1. Aim: Implement a function that uses *args to find the maximum number among the
arguments passed.
2. Program Code: def find_maximum(*args):
3. if not args:
raise ValueError("No arguments provided.")
return max(args)
print("Maximum number:", find_maximum(1, 5, 3, 9, 2))
print("Maximum number:", find_maximum(10, 20, 5))
4. Screenshot of output