0% found this document useful (0 votes)
9 views4 pages

Lab Assignment 8 Solutions With Student Details

The document contains a lab assignment with ten programming questions related to tuples and lists in Python. Each question includes a solution, test cases, and expected output. The topics covered include tuple product, merging tuples, zipping lists, sorting, and generating cubes, among others.

Uploaded by

hk644578
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)
9 views4 pages

Lab Assignment 8 Solutions With Student Details

The document contains a lab assignment with ten programming questions related to tuples and lists in Python. Each question includes a solution, test cases, and expected output. The topics covered include tuple product, merging tuples, zipping lists, sorting, and generating cubes, among others.

Uploaded by

hk644578
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/ 4

Lab Assignment 8

Name: Harsh Kumar

Roll No: 24104032

Group: B5A

Q1. Write a program to find the product of all elements in tuples.

# 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

Q2. Write a function to merge two tuples and remove duplicates.

# Solution to Q2

def merge_tuples(t1, t2):

return tuple(set(t1 + t2))

# Test

t1 = (1, 2, 3)

t2 = (3, 4, 5)

result = merge_tuples(t1, t2)


# Output: (1, 2, 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

for num in args:

total_product *= num

return total_sum, total_product

# Test

result = sum_and_product(1, 2, 3, 4)

# Output: (10, 24)

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

def zip_and_unzip_lists(list1, list2):

zipped = list(zip(list1, list2))

list1_unzipped, list2_unzipped = zip(*zipped)

return zipped, list(list1_unzipped), list(list2_unzipped)

# Test

list1 = [1, 2, 3]

list2 = ['a', 'b', 'c']

result = zip_and_unzip_lists(list1, list2)

# 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):

return sorted(tuples, key=lambda x: x[1])

# Test

tuples = [(1, 3), (2, 1), (3, 2)]

result = sort_tuple_of_tuples(tuples)

# Output: [(2, 1), (3, 2), (1, 3)]

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 = []

for sandwich in sandwich_orders:

print(f"I made your {sandwich} sandwich.")

finished_sandwiches.append(sandwich)

return finished_sandwiches

# Test

sandwich_orders = ['tuna', 'egg', 'chicken']

finished_sandwiches = make_sandwiches(sandwich_orders)

# Output: ["I made your tuna sandwich.", ...]

Q7. Use a list comprehension to generate a list of the first 10 cubes.

# Solution to Q7

cubes = [x**3 for x in range(1, 11)]


# Output: [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]

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

return tuple(str(x) for x in t)

# Test

t = (1, 2, 3)

result = convert_tuple_to_strings(t)

# Output: ('1', '2', '3')

Q9. Use a nested list comprehension to find all of the numbers from 1-100 that are divisible by any s

# Solution to Q9

numbers_divisible = [x for x in range(1, 101) if any(x % d == 0 for d in range(2, 10))]

# Output: [2, 3, 4, 5, ..., 99, 100]

Q10. Write a Python program to find all numbers between 1 and 100 divisible by any number from 2

# Solution to Q10

numbers_divisible = [x for x in range(1, 101) if any(x % d == 0 for d in range(2, 10))]

# Output: [2, 3, 4, 5, ..., 99, 100]

You might also like