About These Assignments
This page contains 30+ unique practice assignments covering all tuple concepts in Python . Unlike the topic-specific pages, these problems are designed to test your ability to choose the right tuple technique for each situation and combine tuple operations effectively.
š¢ Beginner
š Intermediate
š“ Advanced
šÆ Unique Challenge: Each problem is designed to be different from the topic-specific assignments. You'll need to decide which tuple technique (methods, slicing, unpacking, etc.) is most appropriate for each scenario.
š” Tip: Try each problem yourself first. Click "Show Solution" only after attempting.
1. Tuple Creation Challenges 4 Questions
šÆ Focus: Create tuples using different techniques.
Beginner Q1: Create Tuple from Range
Create a tuple containing all odd numbers from 1 to 19 using the range function.
ā¶ Show Solution
š” Solution:
odds = tuple(range(1, 20, 2))
print(odds) # (1, 3, 5, 7, 9, 11, 13, 15, 17, 19)
š Explanation: range(start, stop, step) with tuple() constructor.
Beginner Q2: Tuple from List
Convert a list of numbers into a tuple.
ā¶ Show Solution
š” Solution:
numbers = [10, 20, 30, 40, 50]
tup = tuple(numbers)
print(tup) # (10, 20, 30, 40, 50)
š Explanation: tuple() constructor converts iterable to tuple.
Intermediate Q3: Nested Tuple
Create a nested tuple (tuple of tuples) representing a 2x3 matrix.
ā¶ Show Solution
š” Solution:
matrix = ((1, 2, 3), (4, 5, 6))
print(matrix) # ((1, 2, 3), (4, 5, 6))
š Explanation: Tuples can contain other tuples as elements.
Advanced Q4: Single Element Tuple
Create a tuple with a single element (the number 42) and verify its type.
ā¶ Show Solution
š” Solution:
single = (42,)
print(f"Value: {single}, Type: {type(single)}")
# Value: (42,), Type:
š Explanation: A trailing comma is required for a single-element tuple.
2. Access & Slicing Challenges 4 Questions
šÆ Focus: Access and slice tuples effectively.
Beginner Q5: Access Elements
Given a tuple, access the first, third, and last elements.
ā¶ Show Solution
š” Solution:
tup = (10, 20, 30, 40, 50, 60)
first = tup[0]
third = tup[2]
last = tup[-1]
print(f"First: {first}, Third: {third}, Last: {last}")
š Explanation: Indexing works same as lists.
Beginner Q6: Slice Tuple
Extract the middle four elements from a tuple of 8 elements.
ā¶ Show Solution
š” Solution:
tup = (1, 2, 3, 4, 5, 6, 7, 8)
middle = tup[2:6]
print(middle) # (3, 4, 5, 6)
š Explanation: Slicing works same as lists.
Intermediate Q7: Reverse Tuple
Reverse a tuple using slicing (without using reversed() function).
ā¶ Show Solution
š” Solution:
tup = (1, 2, 3, 4, 5)
reversed_tup = tup[::-1]
print(reversed_tup) # (5, 4, 3, 2, 1)
š Explanation: Step -1 reverses the tuple.
Advanced Q8: Every Third Element
Extract every third element from a tuple starting from index 0.
ā¶ Show Solution
š” Solution:
tup = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
every_third = tup[::3]
print(every_third) # (1, 4, 7, 10)
š Explanation: Slicing with step 3.
3. Tuple Methods Challenges 4 Questions
šÆ Focus: Use built-in tuple methods effectively.
Beginner Q9: Count and Index
Find how many times 5 appears and its first index using count() and index().
ā¶ Show Solution
š” Solution:
tup = (1, 5, 3, 5, 7, 5, 9, 5)
count = tup.count(5)
first_index = tup.index(5)
print(f"Count: {count}, First index: {first_index}")
š Explanation: count() returns occurrences, index() returns first position.
Beginner Q10: Find Element or Handle Error
Find the index of element 42 in a tuple, handle ValueError if not found.
ā¶ Show Solution
š” Solution:
tup = (10, 20, 30, 40, 50)
try:
index = tup.index(42)
print(f"Found at index: {index}")
except ValueError:
print("42 not found in tuple")
š Explanation: index() raises ValueError if element not found.
Intermediate Q11: Check Membership
Check if a given element exists in a tuple using the 'in' operator.
ā¶ Show Solution
š” Solution:
tup = ("apple", "banana", "cherry", "date")
item = "banana"
if item in tup:
print(f"{item} found in tuple")
else:
print(f"{item} not found")
š Explanation: 'in' operator checks membership in tuple.
Advanced Q12: Tuple Length and Min/Max
Find the length, minimum, and maximum of a tuple of numbers.
ā¶ Show Solution
š” Solution:
tup = (25, 18, 42, 7, 31, 56, 19)
length = len(tup)
minimum = min(tup)
maximum = max(tup)
print(f"Length: {length}, Min: {minimum}, Max: {maximum}")
š Explanation: len(), min(), max() work on tuples.
4. Iteration Challenges 4 Questions
šÆ Focus: Iterate over tuples effectively.
Beginner Q13: Sum with For Loop
Calculate the sum of all elements in a tuple using a for loop.
ā¶ Show Solution
š” Solution:
tup = (10, 20, 30, 40, 50)
total = 0
for num in tup:
total += num
print(f"Sum: {total}")
š Explanation: Iterates through each element and accumulates sum.
Beginner Q14: Print Index and Value
Print both index and value for each element using enumerate.
ā¶ Show Solution
š” Solution:
tup = ("apple", "banana", "cherry", "date")
for index, fruit in enumerate(tup):
print(f"{index}: {fruit}")
š Explanation: enumerate() provides index-value pairs.
Intermediate Q15: Tuple of Tuples Iteration
Iterate over a tuple of tuples and print each inner tuple's elements.
ā¶ Show Solution
š” Solution:
data = ((1, 2, 3), (4, 5, 6), (7, 8, 9))
for inner in data:
for item in inner:
print(item, end=' ')
print()
š Explanation: Nested for loops to traverse all levels.
Advanced Q16: While Loop Iteration
Iterate over a tuple using a while loop and print each element squared.
ā¶ Show Solution
š” Solution:
tup = (1, 2, 3, 4, 5)
i = 0
while i < len(tup):
print(tup[i] ** 2, end=' ')
i += 1
# Output: 1 4 9 16 25
š Explanation: While loop with index to access tuple elements.
5. Unpacking Challenges 4 Questions
šÆ Focus: Unpack tuples effectively.
Beginner Q17: Basic Unpacking
Unpack a tuple of three elements into three variables.
ā¶ Show Solution
š” Solution:
tup = (10, 20, 30)
a, b, c = tup
print(f"a: {a}, b: {b}, c: {c}")
š Explanation: Tuple unpacking assigns each element to a variable.
Beginner Q18: Swap Variables
Swap two variables using tuple unpacking.
ā¶ Show Solution
š” Solution:
x, y = 5, 10
print(f"Before: x={x}, y={y}")
x, y = y, x
print(f"After: x={x}, y={y}")
š Explanation: Tuple unpacking for variable swapping.
Intermediate Q19: Extended Unpacking with *
Unpack a tuple into variables using * for the remaining elements.
ā¶ Show Solution
š” Solution:
tup = (1, 2, 3, 4, 5)
first, second, *rest = tup
print(f"First: {first}, Second: {second}, Rest: {rest}")
š Explanation: * collects remaining elements into a list.
Advanced Q20: Nested Unpacking
Unpack a nested tuple (tuple of tuples).
ā¶ Show Solution
š” Solution:
data = ((1, 2), (3, 4), (5, 6))
for a, b in data:
print(f"a={a}, b={b}")
š Explanation: Nested unpacking in loop.
6. Advanced Tuple Challenges 4 Questions
šÆ Focus: Combine multiple tuple techniques.
Advanced Q21: Tuple Concatenation
Concatenate two tuples and find the common elements.
ā¶ Show Solution
š” Solution:
t1 = (1, 2, 3, 4, 5)
t2 = (4, 5, 6, 7, 8)
combined = t1 + t2
common = tuple(set(t1) & set(t2))
print(f"Combined: {combined}")
print(f"Common: {common}")
š Explanation: + for concatenation, set intersection for common elements.
Advanced Q22: Sort Tuple
Sort a tuple without converting to list (use sorted()).
ā¶ Show Solution
š” Solution:
tup = (5, 2, 8, 1, 9, 3)
sorted_tup = tuple(sorted(tup))
print(sorted_tup) # (1, 2, 3, 5, 8, 9)
š Explanation: sorted() returns list, tuple() converts back.
Advanced Q23: Tuple of Dictionaries
Create a tuple of dictionaries and access values.
ā¶ Show Solution
š” Solution:
data = ({"name": "Alice", "age": 25}, {"name": "Bob", "age": 30})
for person in data:
print(f"Name: {person['name']}, Age: {person['age']}")
š Explanation: Tuples can contain dictionaries as elements.
Advanced Q24: Filter Tuple
Create a new tuple containing only even numbers from the original tuple.
ā¶ Show Solution
š” Solution:
tup = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
evens = tuple(x for x in tup if x % 2 == 0)
print(evens) # (2, 4, 6, 8, 10)
š Explanation: Generator expression with tuple() conversion.
Try It Yourself!
Use the interactive editor below to test your solutions or write your own tuple code.
========================================
TUPLE PRACTICE
========================================
Numbers: (1, 2, 3, 4, 5)
Middle elements: (30, 40, 50)
Count of 'red': 2
Index of 'blue': 2
Unpacked: 1, 2, 3, 4, 5
ā
Write your tuple solutions here!
š Related Tutorials