11.2 Creating Tuples
Empty tuples, why a single-element tuple requires a trailing comma, tuple literals, the tuple() constructor, and nested or mixed-type tuples.
Empty Tuple
>>> t = ()
>>> t
()
Single-Element Tuple
A tuple with exactly one element requires a trailing comma — (1) is not a tuple, it’s just the int 1 wrapped in redundant parentheses. This is one of the most common tuple mistakes; see 11.13 Common Mistakes.
>>> single = (1,) # the comma is what makes it a tuple
>>> type(single)
<class 'tuple'>
>>> not_a_tuple = (1) # no comma -- this is just an int
>>> type(not_a_tuple)
<class 'int'>
Tuple Literals
>>> coordinates = (3, 4, 5)
tuple() Constructor
Converts any iterable into a tuple — covered in depth in 8.6 Collection Type Conversion.
>>> tuple([1, 2, 3])
(1, 2, 3)
>>> tuple("abc")
('a', 'b', 'c')
Nested Tuples
>>> nested = ((1, 2), (3, 4))
Mixed Data Types
>>> record = (1, "two", 3.0, True)
Quick Interview Answer
“A tuple literal is just comma-separated values, parentheses optional except for one critical case: a single-element tuple requires a trailing comma —
(1,)is a tuple,(1)is just the int1, since parentheses alone are only grouping, not tuple syntax.tuple()converts any iterable, the same aslist(), including splitting a string into its individual characters. Tuples nest and mix types exactly like lists do, since every slot is just a reference regardless of what it points to.”
Common Mistakes
- Writing
(1)when a one-element tuple was intended — it silently produces anint, not atuple, with no error to flag the mistake. - Forgetting that the comma, not the parentheses, is what actually creates a tuple —
1, 2, 3(no parentheses at all) is still a valid tuple literal. - Calling
tuple("hello")expecting a single-element tuple containing the whole string — it produces one element per character, the same behavior aslist()on a string.
Add More Questions to This Guide
Know a question that should be here? Share it and help the community!
Open Google Form