After first, *rest = (1, 2, 3), what is rest?
A starred name always collects into a list, whatever was unpacked.
Tuples · Exercise 1 of 4
Fifteen multiple-choice questions on Python tuples: creation, single-element tuples, immutability, unpacking, methods, hashability, comparison and namedtuple. Pick an option to check your answer instantly.
After first, *rest = (1, 2, 3), what is rest?
A starred name always collects into a list, whatever was unpacked.
What does collections.namedtuple add over a plain tuple?
It stays an immutable tuple, so _replace() returns a new object rather than changing it.
How do you get a tuple with 4 added to t = (1, 2, 3)?
Only tuple + tuple is allowed, and it creates a new tuple.
With t = ([1],), what does t[0] += [2] do?
+= first extends the list in place, then tries to store the result in t[0], which a tuple forbids. Use t[0].extend([2]) instead.
Which data fits a tuple better than a list?
A coordinate is a fixed-size record whose positions have meaning – the classic tuple. The others grow and shrink.
Timed online tests with instant scores and explanations.