0% found this document useful (0 votes)
5 views

Python Ws 1.3

python

Uploaded by

maivagii70
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Python Ws 1.3

python

Uploaded by

maivagii70
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

WORKSHEET 1.3

Student Name: Tamanna Jha UID: 23BCS13217

Branch: BE-CSE Section/Group: 804-B

Semester: 3rd Date of Performance: 25/07/2024

Subject Name: Python Programming Subject Code: 23CSP-201

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

return sum_result, difference_result, product_result, quotient_result


except ZeroDivisionError as e:
print(e)
return None, None, None, None
num1 = 20
num2 = 2
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

results = arithmetic_operations(num1, num2)

print("Sum:", results[0])

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):
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))
3. Screenshot of output

You might also like