Tuples: Practice Questions
100 questions. Try each one yourself before checking the answer.
Short on time? Filter by Must Do for the 25 questions that cover this topic on their own.
Create a tuple called point holding 10 and 20, then print it and its type.
Create a tuple holding an int, a float, a string and a bool, then print it. Like a list, one tuple can mix types.
Create an empty tuple and print it with its length, then create a tuple from the list [1, 2, 3] using tuple().
Using colours = ("red", "green", "blue"), print the first item and the last item.
Using scores = (88, 92, 79, 95, 60), print how many items it holds and the value in the middle.
Using numbers = (10, 20, 30, 40, 50), print the first three items and the last two using slices. Note that slicing a tuple gives back another tuple.
Using the same tuple, print it reversed with a slice, and print every second item.
Using days = ("Mon", "Tue", "Wed"), print whether "Tue" is in it and whether "Sun" is not in it.
Show that a tuple cannot be edited: write the line that would fail as a comment, explain the error, then produce a changed version by building a new tuple.
Show that a tuple has no .append(), .remove() or .sort(), and explain in a comment why that is the whole point of the type.
Create a tuple holding only the number 5. Print its type, then print the type of (5) and explain the difference in a comment.
Create a tuple without using brackets at all, by writing record = "Asha", 22, "Pune". Print it and its type.
Given record = ("Asha", 22, "Pune"), unpack it into three variables name, age and city on one line, then print each.
Swap two variables using tuple packing and unpacking, and explain in a comment what Python actually does with the right-hand side.
Show what happens when the number of variables does not match the number of items, and explain the error in a comment.
Using votes = ("yes", "no", "yes", "yes", "no"), print how many "yes" votes there are and the position of the first "no".
Using colours = ("red", "green", "blue"), print each colour on its own line with a for loop.
Using the same tuple, print each colour with its position, like 1. red.
Given a = (1, 2) and b = (3, 4), join them with + and repeat a three times with *. Print both results and show a is unchanged.
Create person = ("Asha", (12, 5, 2001)) where the second item is a date. Print the whole tuple, just the date, and just the year.
Convert the list ["a", "b", "c"] into a tuple, then convert it back into a list. Print the type at each step.
Turn the word "python" into a tuple of its characters, then print the tuple and its length.
Print whether (1, 2, 3) equals (1, 2, 3), and whether it equals [1, 2, 3]. Explain the second result.
Using marks = (78, 92, 65, 88, 71), print the total, highest, lowest and average. All the list aggregation functions work on tuples too.
Create the same three values as both a list and a tuple. Add an item to the list, show the tuple cannot do the same, and print both.
Write min_max(numbers) that returns both the smallest and largest value. Unpack the result into two variables, then explain in a comment what the function actually handed back.
Call the same min_max() but store the result in one variable instead of unpacking it. Print the variable, its type, and each item by index.
Write stats(numbers) returning the count, total and average as a tuple. Print the result both unpacked and as a whole.
Write describe(point) that takes a (x, y) tuple and prints a sentence, unpacking the parameter inside the function body.
Given numbers = (1, 2, 3, 4, 5), unpack the first value into first and everything else into rest using a star. Print both and the type of rest.
Using the same tuple, unpack everything except the last item into start, and the last into last.
Given scores = (10, 20, 30, 40, 50), capture the first, the last, and everything between them in one statement.
Given person = ("Asha", (12, 5, 2001)), unpack the name and the three date parts in a single statement using nested brackets.
Build a list of (name, mark) tuples for three students by appending in a loop over two given lists, then print the list.
Loop over records = [("Asha", 88), ("Raj", 74), ("Meera", 91)], unpacking each tuple in the for statement itself, and print an aligned line per student.
Using the same records, split them back into two separate lists of names and marks.
Sort records = [("Raj", 74), ("Asha", 88), ("Meera", 91)] with plain sorted() and explain in a comment which field decided the order.
Sort scores = [(88, "Asha"), (74, "Raj"), (88, "Bala"), (91, "Meera")] and explain how the tie between the two 88s is broken.
Given records = [("Asha", 88), ("Raj", 74), ("Meera", 91)], sort them by mark by rebuilding each tuple with the mark first, sorting, then printing the name first again.
Sort a tuple of numbers. Show that sorted() works on it but hands back a list, then convert the result back to a tuple.
"Change" the tuple (10, 20, 30) so the middle value becomes 99, by converting to a list, editing, and converting back. Print the old and new tuples.
Print the result of comparing (1, 2, 3) with (1, 2, 4), (1, 3) and (1, 2). Explain the rule in a comment.
Given a list of (item, price) tuples, find and print the most expensive item's name and price.
Store a 3x3 grid as a tuple of tuples, print it row by row, and print the total of every number.
Store a fixed configuration as a tuple of constants and use it. Explain in a comment why a tuple is the better choice than a list here.
Write a function that takes a full name as a single string and returns first name, middle names and last name as a tuple, coping with any number of middle names.
Given a list of (name, mark) records, build two new lists โ one of records that passed (40 or more) and one that failed โ then print both counts.
Given a list of (code, city) tuples, ask the user for a code and print the matching city, or a not-found message.
Given a list of (item, quantity, price) tuples, print an aligned table with a line total per row and a grand total.
Write divide_with_remainder(a, b) that returns the quotient and remainder as a tuple, and use it inside a loop, unpacking the result each time.
Ask how many students, collect a name and mark for each into a list of tuples, then print the class report with the average.
Write distance(p1, p2) that takes two (x, y) tuples and returns the straight-line distance between them, without importing anything.
Write midpoint(p1, p2) returning the halfway point as a tuple, and quadrant(point) returning which quadrant a point sits in.
Given a list of (x, y) points, find the one closest to the origin and print it with its distance.
Given a list of (name, r, g, b) colour tuples, print each colour's total brightness and name the brightest.
Write format_date(date) that takes a (day, month, year) tuple and returns a string like 12 May 2001, and is_valid(date) that checks the day and month are in range.
Write to_seconds(time) taking an (h, m, s) tuple, and to_hms(seconds) returning one. Show a round trip.
Given a list of (item, quantity, unit_price) tuples, print the total value of the stock and the single most valuable line.
Given a list of (name, score) records, print a leaderboard sorted highest first, giving tied scores the same rank.
Given a list of "name,age,city" strings, turn each into a tuple of three fields and print an aligned table.
Given a list of (day, temperature) tuples, print the hottest day, the coldest day, and every day above the average.
Write transpose(grid) that takes a tuple of tuples and returns a new tuple of tuples with rows and columns swapped.
Given a list of (name, department, salary) tuples, print the average salary and everyone earning above it.
Given a list of records where some are exact duplicates, build a list of the unique ones keeping the original order, and report how many were dropped.
Given two lists of (name, mark) records from two classes, merge them into one sorted list and print it with a running position number.
Given a list of (title, author, year) book records, print only the books published after a year the user chooses.
Given a list of full names as strings, build a list of (first, last) tuples and print them sorted by last name.
Given (name, mark1, mark2, mark3) records, build a new list of (name, total, average, grade) tuples and print it as a table.
Write summarise(records) that takes a list of (name, value) tuples and returns a tuple of (count, total, highest_name, lowest_name). Use it and unpack the result.
Write print_table(headers, rows) where headers is a tuple of column names and rows is a list of tuples, sizing every column to its widest entry.
A tuple is supposed to be unchangeable, yet this code changes one. Explain exactly what is and is not protected.
record = ("Asha", [88, 92])
record[1].append(79)
print(record)This function is meant to return a one-item tuple but returns something else entirely. Find the bug and show the fix.
def wrap(value):
return (value)
result = wrap(5)
print(result)
print(type(result))
print(len(result))The opposite mistake: this is meant to store the number 5, but doubling it gives a strange answer. Explain and fix.
value = 5,
print(value)
print(value * 2)Write safe_unpack(record) that unpacks a (name, mark) record but reports a clear message when the record has the wrong number of fields, without letting the program crash.
Show that += behaves differently on a tuple and a list, using id() to prove whether a new object was made.
Show that a tuple can be hashed but a list cannot, and explain in a comment why that difference is about to matter.
Given a = (1, 2, 3) and b = (1, 2, 3), print a == b and a is b, then explain which one you should use and why the second might surprise you.
Show that comparing tuples of different lengths works fine, but comparing tuples holding different types can fail. Explain the rule.
This should print each student on a tidy line but prints brackets and quotes instead. Find and fix it.
records = [("Asha", 88), ("Raj", 74)]
for record in records:
print(f"{record} scored well")This is meant to hold three records but len() says four. Find the bug.
records = [
("Asha", 88),
("Raj", 74),
("Meera", 91),
]
extra = [("Dev", 66)]
everyone = records + extra
print(len(everyone))
names = ("Asha"), ("Raj"), ("Meera")
print(len(names))Sort a list of (name, score) records by score descending, breaking ties by name ascending โ without using sorted(key=โฆ).
Given a list of (name, weight) records sorted by weight, use two pointers to find a pair whose weights add up exactly to a target.
Write a recursive function flatten(data) that takes a tuple which may contain other tuples at any depth, and returns one flat tuple of all the values.
Write sort_by_field(records, index) that sorts a list of tuples by any chosen position, using a selection sort you write yourself.
Design question. Write one function that returns a tuple and one that returns a list of the same data, then explain in comments which you would choose for a fixed set of results and why.
Build a Student Records Manager. Offer a menu to add a (name, mark) record, list all records sorted by mark, show class statistics, and quit.
Build a Contact Book holding (name, phone, email) tuples. Offer add, search by any part of a name, list all, and quit.
Build a Coordinate Geometry Toolkit with distance(p1, p2), midpoint(p1, p2) and perimeter(points), then report on a shape the user describes.
Build an Inventory Value Report from (item, quantity, unit_price) records: an aligned table with line totals, the grand total, the most valuable line, and a low-stock warning under 10 units.
Build a Tournament Leaderboard. Read (player, wins, losses) records, compute points as 3 per win, and print a ranked table with win percentage, giving equal points the same rank.
Build a Timetable as a tuple of tuples โ one row per day, one column per period โ and print it as a grid, then report which day has the most free periods.
Build a Movie Ratings Report from (title, year, rating) records: list them sorted by rating, show the average, and let the user filter by minimum rating.
Build a Bank Transaction Log. Store each entry as a (kind, amount) tuple, keep a running balance, refuse an overdraft, and print a statement at the end.
Build a Distance Matrix. Store city names in a tuple and distances in a tuple of tuples, print the matrix as a grid, and report the closest pair of cities.
Build an Election Vote Counter. Keep the candidates in a tuple, read votes until the user types done, reject invalid names, then print a tally sorted by votes with percentages and the winner.
Compare tuples and lists properly: mutability, available methods, hashability, and what each one signals to a reader. Demonstrate every difference in code.
Packing and unpacking show up all over Python, often without brackets. Demonstrate every place you have now met, and name each one.
Explain why "tuples are immutable" is only half true. Show what is genuinely guaranteed, what is not, and how to build a fully frozen structure.
Tuples make decent records until they grow. Show the same data as a tuple record and explain, with code, where positional records start to hurt and what fixes it.
Capstone. Build a Tuple Toolkit Report. Read name,mark entries until the user types done, storing each as a tuple, then print one report showing the records, the same records sorted by mark and by name, the statistics as an unpacked tuple, the highest and lowest performers, any duplicate entries, and the records split into passed and failed.
Still stuck on something?
Book a free 1-on-1 session and we'll work through it together.
Book a Free Session