Pimpri Chinchwad Education Trust’s
Pimpri Chinchwad College of Engineering
Assignment-5
Department: MCA Academic Year: 2024-2025 Maximum Marks:100
Semester: I Year and Div.: FYMCA
Subject: Python Programming Lab Subject Teacher: Prof. Rajkamal Sangole
Name & sign
Assignment No: 5 Assignments Date: Date of Submission:
based on Functions in Python 12/10/2024
2
1. Create a Python function that calculates the area of a rectangle. The function should have
default arguments for the width and height. Test the function with and without providing the
arguments.
Solution:
def calculate_rectangle_area(width=1, height=1):
area = width * height
return area
default_area = calculate_rectangle_area()
print(f"Area with default arguments: {default_area}")
custom_area = calculate_rectangle_area(5, 10)
print(f"Area with width 5 and height 10: {custom_area}")
Output:
2. Develop a Python function that takes a list of numbers and returns the minimum, maximum,
and average of the list. Call the function and print the results.
Solution:
def check_list(number):
avrage=sum(number)/len(number)
maximum=max(number)
minimum=min(number)
return avrage,maximum,minimum
list=[3,4,5,6,7,8]
avg,maxi,mini=check_list(list)
print("Avrage: ",avg)
print("Maximum: ",maxi)
print("Minimum: ",mini)
Output:
3. Create a Python function that accepts any number of arguments and returns their product.
Test the function with different numbers of arguments.
Solution:
def check_product(* args):
result=1
for i in args:
result*=i
return result
print(check_product(2,3))
print(check_product(3))
print(check_product(2,3,4,5))
print(check_product())
Output:
4. Write a Python program that uses a lambda function to sort a list of tuples based on the
second element of each tuple. Test the sorting function with sample data.
Solution:
data = [(1, 4), (3, 0), (2, 9), (4, 1)]
sorted_data = sorted(data, key=lambda x: x[1])
print("Sorted list based on the second element of each tuple:")
print(sorted_data)
Output:
5. Develop a Python function that calculates the factorial of a number using recursion. Test the
function with various inputs.
Solution:
def fact(num):
if num==0:
return 1
fac=num*fact(num-1)
return fac
num=int(input("Enter The Number: "))
print(fact(num))
Output:
6. Write a Python function that takes a list of numbers as an argument and returns a new list
with each element squared. Call the function and print the new list.
Solution:
def new_list(numbers):
newlist=[]
for num in numbers:
[Link](num**2)
return newlist
num=[3,4,5,6,7]
num1=new_list(num)
print(num)
print(num1)
Output:
7. Create a Python function that takes a list of strings and converts each string to uppercase. The
function should modify the original list. Test the function with a sample list.
Solution:
string=['hi','my','name','is','dhanashri','godge']
print(string)
for i in range(len(string)):
string[i]=string[i].upper()
print(string)
Output:
8. Develop a Python function that checks whether a given number is prime. The function should
return True if the number is prime and False otherwise.
Solution:
def check_prime(num):
flag=True
for i in range(2,num):
if num%i==0:
flag=False
return flag
num=int (input("Enter The Number: "))
print(check_prime(num))
Output:
9. Write a Python function that takes a dictionary as an argument and prints each key-value pair
in the format key: value. Test the function with a sample dictionary.
Solution:
def print_dect(dict):
print("Key\tValue")
for key,values in [Link]():
print(f"{key}\t{values}")
dict={2:"ram",3:"sham"}
print_dect(dict)
Output:
10. Create a Python function that contains another function inside it. The inner function should
perform a calculation, and the outer function should return the result of the inner function.
Demonstrate the use of the nested function.
Solution:
def outer_function(x, y):
def inner_function(a, b):
return a + b
return inner_function(x, y)
result = outer_function(5, 10)
print("The result of the calculation is:", result)
Output: