0% found this document useful (0 votes)
2 views3 pages

2.tuple Programs Worksheet

The document provides solutions to various Python programming tasks related to tuples, including creation, access, immutability, conversion to lists, and more. Each program demonstrates a specific tuple operation, such as concatenation, repetition, and using tuples in functions and dictionaries. The solutions are presented in a clear format with example code snippets.

Uploaded by

lessuse163
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)
2 views3 pages

2.tuple Programs Worksheet

The document provides solutions to various Python programming tasks related to tuples, including creation, access, immutability, conversion to lists, and more. Each program demonstrates a specific tuple operation, such as concatenation, repetition, and using tuples in functions and dictionaries. The solutions are presented in a clear format with example code snippets.

Uploaded by

lessuse163
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

Python Programs: Tuple (Solutions)

Program 1: Tuple - Creation


• Task: Create a tuple with multiple data types.

Solution:
tuple1 = (10, "apple", 3.14)
print(tuple1)

Program 2: Tuple - Access


• Task: Access elements using positive and negative indexing.

Solution:
tuple1 = ("a", "b", "c")
print(tuple1[0])
print(tuple1[-1])

Program 3: Tuple - Immutability


• Task: Show that tuples are immutable.

Solution:
tuple1 = (1, 2, 3)
# tuple1[0] = 10 # TypeError

Program 4: Tuple - Convert to List


• Task: Update a tuple by converting to a list and back.

Solution:
t = (1, 2, 3)
l = list(t)
l[1] = 200
t = tuple(l)
print(t)

Program 5: Tuple - Loop


• Task: Loop through a tuple using for loop.

Solution:
colors = ("red", "green", "blue")
for color in colors:
print(color)
Program 6: Tuple - Nested Tuple Access
• Task: Access nested tuple values.

Solution:
t = ((1, 2), (3, 4))
print(t[1][0]) # 3

Program 7: Tuple - Count and Index


• Task: Use count() and index() with tuple.

Solution:
t = (1, 2, 2, 3, 4)
print([Link](2))
print([Link](3))

Program 8: Tuple - Concatenation


• Task: Concatenate two tuples.

Solution:
t1 = (1, 2)
t2 = (3, 4)
t3 = t1 + t2
print(t3)

Program 9: Tuple - Repetition


• Task: Repeat tuple elements using * operator.

Solution:
t = (1, 2)
print(t * 3) # (1, 2, 1, 2, 1, 2)

Program 10: Tuple - Check Membership


• Task: Check if a value exists in tuple.

Solution:
t = (10, 20, 30)
print(20 in t) # True

Program 11: Tuple - Tuple in Function


• Task: Use tuple as a function parameter and return type.

Solution:
def min_max(t):
return min(t), max(t)
print(min_max((4, 2, 8)))
Program 12: Tuple - Tuple of Tuples
• Task: Access values in a tuple of tuples.

Solution:
students = (("Ali", 90), ("Sara", 95))
print(students[1][0])

Program 13: Tuple - Use in Dictionary


• Task: Use tuples as keys in a dictionary.

Solution:
grades = {("Ali", "Math"): 95, ("Sara", "Math"): 98}
print(grades[("Ali", "Math")])

You might also like