Ujwal Ass2
Ujwal Ass2
ASSIGNMENT 2
Ujwal.C
1602-23-744-009
ES-VLSID
1. Write a function that demonstrates the use of multiple except blocks by handling
both ValueError and TypeError.
Prog:
def handle_errors(x, y):
try:
result = x / y
print(result)
except ValueError:
print("ValueError: Invalid input")
except TypeError:
print("TypeError: Input must be a number")
except Exception as e:
print(f"Unexpected error: {e}")
def check_age(age):
if not (18 <= age <= 65):
raise InvalidAgeError("Age must be between 18 and 65")
else:
print("Age is valid")
6. Write a program that takes a list of tuples (name, score) and returns a dictionary
with names as keys and scores as values.
Prog:
def tuple_list_to_dict(tuple_list):
return dict(tuple_list)
7. Create a nested dictionary to store student information (name, age, grade) and
write a function to display the information.
Prog:
# Create a nested dictionary to store student information
students = {
"John": {"age": 18, "grade": 90},
"Jane": {"age": 19, "grade": 85},
"Bob": {"age": 20, "grade": 92}
}
# Difference
difference1 = set1.difference(set2)
difference2 = set2.difference(set1)
print(f"Difference (set1 - set2): {difference1}")
print(f"Difference (set2 - set1): {difference2}")
9. Write a program to demonstrate tuple assignment and swap two variables using
tuple assignment.
Prog:
# Tuple assignment
name, age = ("John", 25)
print(name) # Output: John
print(age) # Output: 25
# Swap a and b
a, b = b, a
print("After swap: a =", a, "b =", b)
# Output:
# Before swap: a = 10 b = 20
# After swap: a = 20 b = 10
10. Write a program that takes a list of numbers as input and returns a tuple
containing the minimum, maximum, and average of the number
prog:
def calculate_stats(numbers):
minimum = min(numbers)
maximum = max(numbers)
average = sum(numbers) / len(numbers)
return (minimum, maximum, average)
# Output:
# (1, 5, 3.0)