Tuples — Practice Questions
50 questions. Try each one yourself before checking the answer.
Create point = (3, 4) and print it along with its type().
Using colors = ("red", "green", "blue"), print the first and last items.
Using numbers = (10, 20, 30, 40, 50), print the first three elements using slicing.
Print the number of items in weekdays = ("Mon", "Tue", "Wed", "Thu", "Fri").
Create point = (3, 4) and try point[0] = 5. Print the error type this raises, in a comment.
Assign the values 1, 2, 3 to a single variable numbers without parentheses (tuple packing), then print it and its type.
Given point = (3, 4), unpack it into x and y, then print both.
Given rgb = (255, 0, 128), print each value using a for loop.
Create a tuple with a single element 5 (remember the trailing comma) and print its type to confirm it's really a tuple.
Given days_off = ("Saturday", "Sunday"), print whether "Monday" is in it.
Given numbers = (4, 8, 2, 8, 5, 8), print how many times 8 appears using .count(), and the index of its first appearance using .index().
Create student = ("Asha", (85, 90, 78)). Print the student's name and their second score.
Given tuple1 = (1, 2, 3) and tuple2 = (4, 5, 6), create a new combined tuple using +, and a repeated tuple using *.
Convert numbers_tuple = (3, 1, 2) into a list, sort it, then convert it back into a tuple. Print the final result.
Given point = (10, 20, 30), unpack it using the "star" syntax so the first value goes to x and the rest go to a list rest.
Swap the values of a = 5 and b = 10 using tuple packing/unpacking (revisit from Operators, now naming the mechanism).
Write a function min_max(numbers) that returns a tuple (minimum, maximum), then unpack the result when calling it.
Given points = [(1, 2), (3, 4), (5, 6)], print each point's x and y separately by unpacking inside a for loop.
Given people = [("Alice", 30), ("Bob", 25), ("Carol", 35)], sort by age using sorted() with a lambda key.
Print the results of comparing (1, 2, 3) == (1, 2, 3), (1, 2, 3) < (1, 2, 4), and (1, 3) > (1, 2, 100), and explain in a comment how tuple comparison works.
Ask the user for two numbers representing coordinates, store them as a tuple point, and print the Euclidean distance from the origin (0, 0).
Store a color as rgb = (200, 100, 50). Print whether the color is "dominant red" (its first component is the largest of the three).
Given records = [("Alice", 88), ("Bob", 45), ("Carol", 92)], print each name with "Pass"/"Fail" based on a 40-point threshold, unpacking each record in the loop.
Ask the user for a month and day and pack them into a date_tuple. Print it formatted as MM/DD.
Given names = ["Alice", "Bob"] and ages = [30, 25], use zip() to build a list of (name, age) tuples, and print it.
Given pairs = [("Alice", 30), ("Bob", 25)], split it back into two separate lists (names, ages) using zip(*pairs).
Write two short functions, sum_list(numbers) and sum_tuple(numbers), that both compute a sum with a loop, showing that the same logic works on either type since both are iterable.
Given login_attempt = ("admin", "wrongpass") representing (username, password), unpack it and print whether it matches the correct credentials ("admin", "admin123").
The following code crashes with a TypeError. Find and fix the bug (without converting the tuple to a list).
scores = (85, 90, 78)
scores[0] += 5
print(scores)Given point = (12.5, -3.2), print it formatted as "(12.50, -3.20)" using an f-string with unpacking.
Given record = ("Asha", (1995, 6, 15)) representing (name, (birth_year, birth_month, birth_day)), unpack all four values in a single statement.
Given three variables a = 1, b = 2, c = 3, rotate their values in ONE line so that a gets b's value, b gets c's value, and c gets a's original value.
Given data = [(3, "c"), (1, "a"), (2, "b")], sort it by the first element of each tuple, then print only the second elements in that new order.
Demonstrate that tuples can be used as dictionary keys while lists cannot, using a small example that maps (row, col) coordinate tuples to values.
Use collections.namedtuple to create a Point type with fields x and y, create an instance, and print both its field access styles: point.x and point[0].
Given intervals = [(1, 3), (2, 6), (8, 10), (15, 18)], merge any overlapping intervals and print the merged result. (Sort first, then walk through with a loop.)
Given transactions = [("deposit", 500), ("withdraw", 200), ("deposit", 100)], compute the final balance starting from 1000, unpacking each transaction in a loop.
Given data = (1, 2, [3, 4]), demonstrate that while the TUPLE itself is immutable, a MUTABLE object stored inside it (the list) can still be changed in place.
Given pairs = [(1, "a"), (2, "b"), (3, "c")], build a "reversed" list where each tuple's elements are swapped, using a loop and unpacking (not a comprehension).
Given a function that should return a fixed 3-value record (name, age, is_active), explain and demonstrate why a tuple is a more appropriate return type here than a list.
Build a "Student Record System" using a list of (name, grade) tuples. Support printing all records and finding the highest-graded student, all via unpacking.
Build a "Coordinate Distance Calculator". Ask for two points as coordinate pairs and print the distance between them using tuple unpacking.
Build an "RGB Color Mixer". Ask for two RGB tuples and print their average color (component-wise), rounded to whole numbers.
Build a small "Contact Database" using a list of (name, phone, email) tuples, with a search function that finds a contact by name.
Build a "Time Duration Formatter" that takes a total number of seconds and returns a tuple (hours, minutes, seconds), then prints it formatted as HH:MM:SS.
A common interview question: "when would you use a tuple instead of a list?" Answer it with two concrete code examples — one where a fixed-shape return value benefits from a tuple, and one where a growing collection clearly needs a list.
Explain why tuples are hashable (and therefore usable as dict keys / set members) while lists are not, and demonstrate the TypeError that results from trying to put a list in a set.
Demonstrate how tuple unpacking makes function calls that return multiple values more readable than manually indexing into a returned collection, comparing both styles side by side.
Explain in a comment why tuples are generally slightly faster to create and iterate over than lists, and demonstrate that a tuple literal is still valid even without wrapping parentheses in specific contexts (like a return statement).
Build a "Tuple Mastery Report" — the most complete demonstration in this module. Given records = [("Alice", 88), ("Bob", 45), ("Carol", 92), ("David", 67)], print: all records unpacked in a formatted table, the record with the highest score, the average score (manual loop), and a new list of (name, "Pass"/"Fail") tuples built without comprehensions.
Still stuck on something?
Book a free 1-on-1 session and we'll work through it together.
Book a Free Session