Playing as a guest? to earn XP and track your progress.
๐ Tuples Quiz
๐ Tuples
Which statement best describes a tuple?
An ordered, mutable collection in [ ]
An unordered set of unique values
An ordered, immutable collection of values in ( )
A collection of key:value pairs
A tuple is an ordered, immutable collection, usually written in round brackets and separated by commas.
Which kind of data is best stored in a tuple rather than a list?
The days of the week
A list of messages that grows
Marks being entered one by one
A shopping cart that keeps changing
Tuples suit data that must stay fixed, such as the days of the week or coordinates.
What is T after running T = 4, 5, 6?
SyntaxError
A tuple (4, 5, 6)
The integer 456
A list [4, 5, 6]
The brackets are optional; the commas make the tuple.
What is tuple("ABC")?
['A', 'B', 'C']
Error
('ABC',)
('A', 'B', 'C')
tuple() converts a sequence into a tuple of its items; a string gives its characters.
What is the type of x after x = (7)?
str
tuple
int
list
(7) is just 7 inside grouping brackets; a tuple needs a comma.
Which statement creates a tuple with ONE element?
T = (5,)
T = [5]
T = tuple(5)
T = (5)
A single-element tuple needs a trailing comma: (5,).
What is printed after b = (5,) and then print(b)?
5
(5,)
[5]
(5)
Python shows a one-element tuple with its trailing comma.
What is the difference between T = (10) and T = (10,)?
(10) is a tuple; (10,) is a list
There is no difference
Both are errors
(10) is an integer; (10,) is a one-element tuple
It is the comma, not the brackets, that creates a tuple.
For T = ('a', 'e', 'i', 'o', 'u'), what does print(T[0], T[2], T[-1]) display?
a e u
e o u
a i u
a i o
T[0] = 'a', T[2] = 'i' and T[-1] = 'u'.
For T = ('a', 'e', 'i', 'o', 'u'), what does T[5] give?
IndexError: tuple index out of range
()
'u'
None
The valid indices are 0 to 4.
What does T[0] = 100 give for T = (10, 20, 30)?
AttributeError
TypeError: 'tuple' object does not support item assignment
IndexError
(100, 20, 30)
Tuples are immutable, so their elements cannot be reassigned.
What does T.append(40) give for a tuple T?
It returns a new tuple
It adds 40 at the end
TypeError
AttributeError: 'tuple' object has no attribute 'append'
Tuples have no append() method.
Which statement is allowed on a tuple T?
T.pop()
del T[0]
del T
T.remove(10)
del T deletes the whole tuple variable; deleting individual items is not allowed.
What is printed after this code?
T = (10, 20, 30) T = T + (40,) print(T)
(10, 20, 30, 40)
TypeError
(10, 20, 30)
[10, 20, 30, 40]
A NEW tuple is created by concatenation and assigned to T.
How can you 'change' the element at index 1 of T = (10, 20, 30) to 200?
T.replace(20, 200)
Convert to a list, change it, then convert back with tuple()
It cannot be done in any way
T[1] = 200
L = list(T); L[1] = 200; T = tuple(L) gives (10, 200, 30).
What is printed after this code?
T = (1, 2, [3, 4]) T[2][0] = 30 print(T)
TypeError
(1, 2, 30)
(1, 2, [3, 4])
(1, 2, [30, 4])
The tuple still holds the same list object; only the contents of that list changed.
For T = (1, 2, [3, 4]), which statement raises a TypeError?
print(T[2])
T[2].append(5)
T[2] = [5, 6]
T[2][0] = 30
Replacing an element of the tuple is not allowed, but the list inside it can be modified.
What is (1, 2) + (3, 4)?
(1, 2, 3, 4)
(4, 6)
Error
((1, 2), (3, 4))
- concatenates two tuples.
What is ("Hi",) * 3?
('HiHiHi',)
('Hi', 'Hi', 'Hi')
'HiHiHi'
Error
- repeats the elements of the tuple.
What is (1, 2) + (3,) * 2?
(1, 2, 3, 1, 2, 3)
Error
(1, 2, 6)
(1, 2, 3, 3)
- is applied first: (3,) * 2 = (3, 3); then + joins the tuples to give (1, 2, 3, 3).
What does T + 3 give for T = (1, 2)?
(4, 5)
(1, 2, (3))
TypeError: can only concatenate tuple (not "int") to tuple
(1, 2, 3)
Only a tuple can be concatenated with a tuple; write T + (3,).
What is the result of (1, 2, 5) < (1, 3)?
True
None
False
Error
Tuples are compared element by element; at index 1, 2 < 3.
For T = (10, 20, 30, 40, 50, 60), what is T[4:1:-1]?
(40, 30, 20)
(50, 40, 30)
()
(50, 40, 30, 20)
Going backwards from index 4: indices 4, 3, 2 (1 is excluded).
For T = (10, 20, 30, 40, 50, 60), what are T[2] and T[2:3]?
(30,) and (30,)
(30,) and 30
30 and 30
30 and (30,)
Indexing returns an element, while slicing always returns a tuple.
For T = (10, 20, 30, 40, 50, 60), what is T[::2]?
(60, 40, 20)
(20, 40, 60)
(10, 30, 50)
(10, 20)
It takes every 2nd element from the start.
For T = ('P', 'Y', 'T', 'H', 'O', 'N'), what is T[-1:-4:-1]?
('N', 'O', 'H')
()
('H', 'O', 'N')
('N', 'O', 'H', 'T')
It starts at -1 (N) and moves backwards to -3 (H); -4 is excluded.
For T = ('P', 'Y', 'T', 'H', 'O', 'N'), what is 'y' in T?
True
False
1
Error
Membership is case-sensitive; T contains 'Y', not 'y'.
What is 'packing' a tuple?
Removing duplicates
Converting a tuple into a list
Sorting a tuple
Putting several values together into a tuple, e.g. student = 101, "Aman", 88
Packing combines several values into one tuple.
What is 'unpacking' a tuple?
Adding elements to a tuple
Deleting a tuple
Assigning the elements of a tuple to an equal number of variables in one statement
Printing a tuple
For example, roll, name, marks = (101, "Aman", 88).
After roll, name, marks = (101, "Aman", 88), what is the value of name?
'Aman'
(101, 'Aman', 88)
101
88
The values are assigned in order: roll = 101, name = 'Aman', marks = 88.
What does a, b = (1, 2, 3) give?
a = (1, 2), b = 3
ValueError: too many values to unpack
a = 1, b = (2, 3)
a = 1, b = 2
The number of variables must equal the number of elements.
What does a, b, c = (1, 2) give?
TypeError
c = None
a = 1, b = 2, c = 0
ValueError: not enough values to unpack
There are fewer values than variables.
The one-line swap a, b = b, a works because of:
A special swap keyword
Lists
Tuple packing on the right and unpacking on the left
The + operator
The right side packs (b, a) into a tuple and the left side unpacks it.
Which are the only two methods available for tuples?
count() and index()
append() and remove()
sort() and reverse()
insert() and pop()
Tuples are read-only, so they have only the two methods that read them: count() and index().
For T = (4, 9, 1, 9, 7), what are T.count(9) and T.index(9)?
1 and 1
2 and 1
2 and 3
9 and 1
9 occurs twice, and its first occurrence is at index 1.
For T = (4, 9, 1, 9, 7), what is sum(T)?
29
31
Error
30
4 + 9 + 1 + 9 + 7 = 30.
What does sorted((3, 1, 2)) return?
Error
None
[1, 2, 3]
(1, 2, 3)
sorted() always returns a LIST, even when it is given a tuple.
How do you get a sorted TUPLE from T = (40, 10, 30, 20)?
tuple(sorted(T))
T.sorted()
sorted(T)
T.sort()
Wrap sorted() in tuple(); tuples have no sort() method.
What does T.sort() give for a tuple T?
It returns None
AttributeError โ tuples have no sort() method
It sorts T in place
It returns a sorted list
Tuples have no sort(), append(), insert(), remove() or pop() methods.
What is the output of print(len((1, (2, 3), [4, 5, 6])))?
2
6
5
3
The nested tuple and the list each count as ONE element.
For results = (("Riya", 92), ("Aman", 85), ("Zoya", 78)), what is results[1][0]?
('Aman', 85)
85
'Aman'
'Riya'
results[1] is ('Aman', 85), and index 0 of that is 'Aman'.
What does this code print first?
for name, marks in (("Riya", 92), ("Aman", 85)): print(name, "scored", marks)
Riya scored 92
name scored marks
Error
('Riya', 92) scored
Each inner tuple is unpacked into name and marks as the loop runs.
Which statement comparing lists and tuples is correct?
Tuples are mutable and lists are immutable
Lists have many methods; tuples have only count() and index()
Lists can be used as dictionary keys
Tuples are slower than lists
Lists are mutable with many methods; tuples are immutable, slightly faster, and can be dictionary keys.
Which is an advantage of tuples over lists?
They can be changed easily
They use square brackets
They have more methods
Their data is protected from accidental changes
Tuples are read-only, so values like the months of the year cannot be changed by mistake.
Which can be used as a dictionary key: a tuple of immutable values or a list?
Neither
Only a list
Only a tuple (of immutable values)
Both
Tuples of immutable values can be dictionary keys; lists cannot.
What is printed after this code?
T = (10, 20, 30, 40, 50) print(T[1:4], T[-2], T[::-2])
(20, 30, 40) 40 (50, 30, 10)
(10, 20, 30) 40 (50, 40, 30)
(20, 30) 40 (10, 30, 50)
(20, 30, 40) 50 (50, 30, 10)
Indices 1-3 give (20, 30, 40); T[-2] is 40; the reverse step of 2 gives (50, 30, 10).
What is printed after this code?
T = (10, 20, 30, 40, 50) a, b, c = T[:3] print(a + b + c, T.index(c))
30 2
60 2
Error
60 3
T[:3] is (10, 20, 30); their sum is 60, and 30 is at index 2.
What is printed after this code?
T1 = (1, 2, [3, 4]) T1[2].append(5) print(T1)
(1, 2, [3, 4], 5)
(1, 2, 5)
TypeError
(1, 2, [3, 4, 5])
The list inside the tuple is mutable, so append() works on it.
What is printed after this code?
T3 = ("A",) * 2 + ("B",) print(T3, T3.count("A"), T3.index("B"))
('A', 'B', 'A', 'B') 2 1
('A', 'A', 'B') 2 1
('A', 'A', 'B') 2 2
('AA', 'B') 1 1
T3 is ('A', 'A', 'B'); 'A' occurs twice, and 'B' is at index 2.
In a tuple program, how do you 'add' an element x to T?
T = T + (x,)
T.append(x)
T += x
T.insert(x)
Create a new tuple with concatenation โ and remember the comma in (x,).
For the input (10, 20, 30, 40, 50), what tuple of 'elements above the mean' is printed?
(10, 20)
(40, 50)
(50,)
(30, 40, 50)
The mean is 30.0; only 40 and 50 are greater than it.