Lab Assignment 8 Solutions With Student Details
Lab Assignment 8 Solutions With Student Details
Group: B5A
# Solution to Q1
def product_of_tuple_elements(t):
product = 1
for num in t:
product *= num
return product
# Test
t = (2, 3, 4)
result = product_of_tuple_elements(t)
# Output: 24
# Solution to Q2
# Test
t1 = (1, 2, 3)
t2 = (3, 4, 5)
Q3. Write a Python function that accepts a variable number of arguments and returns a tuple contai
# Solution to Q3
def sum_and_product(*args):
total_sum = sum(args)
total_product = 1
total_product *= num
# Test
result = sum_and_product(1, 2, 3, 4)
Q4. Write a Python program to zip two lists into a list of tuples and then unzip the tuples back into s
# Solution to Q4
# Test
list1 = [1, 2, 3]
# Output: ([(1, 'a'), (2, 'b'), (3, 'c')], [1, 2, 3], ['a', 'b', 'c'])
Q5. Write a program to sort tuple of tuples by second item.
# Solution to Q5
def sort_tuple_of_tuples(tuples):
# Test
result = sort_tuple_of_tuples(tuples)
Q6. Make a tuple of list called sandwich_orders and fill it with the names of various sandwiches. Th
# Solution to Q6
def make_sandwiches(sandwich_orders):
finished_sandwiches = []
finished_sandwiches.append(sandwich)
return finished_sandwiches
# Test
finished_sandwiches = make_sandwiches(sandwich_orders)
# Solution to Q7
Q8. Write a program to convert a tuple of integers to a tuple of strings using list comprehension.
# Solution to Q8
def convert_tuple_to_strings(t):
# Test
t = (1, 2, 3)
result = convert_tuple_to_strings(t)
Q9. Use a nested list comprehension to find all of the numbers from 1-100 that are divisible by any s
# Solution to Q9
Q10. Write a Python program to find all numbers between 1 and 100 divisible by any number from 2
# Solution to Q10