Sub topic 2
Creating tuples, and why the comma matters
This chapter assumes you know basic Python syntax, variables, lists and indexing. It covers how Python decides that an expression is a tuple, and the bugs that come from getting that wrong.
The comma builds the tuple
A tuple is an ordered, immutable sequence. Python creates one when it sees commas. Parentheses are usually only there to group the items and make the code easier to read.
point = (3, 4)
single = (3,)
not_a_tuple = (3)
no_parens = 1, 2, 3
print(type(point), type(single), type(not_a_tuple))
print(no_parens)This prints <class 'tuple'> <class 'tuple'> <class 'int'>, then (1, 2, 3). The value (3) is the integer 3 in grouping parentheses, the same as in (1 + 2) * 4. The value (3,) is a tuple, and 1, 2, 3 is a tuple with no parentheses at all. This is called packing, and a later chapter uses it for unpacking.
Empty and one-element tuples
The empty tuple is the only case where parentheses alone create a tuple, because there is no comma to look for.
empty = ()
also_empty = tuple()
single = ("only",)
print(len(empty), len(also_empty), len(single))This prints 0 0 1. A one-element tuple always needs its trailing comma. When you print one, Python shows the comma too: (3,).
Converting other iterables
The tuple() constructor accepts any iterable and returns a new tuple of its items.
print(tuple([1, 2, 3]))
print(tuple("abc"))
print(tuple(n * n for n in range(4)))The output is (1, 2, 3), ('a', 'b', 'c') and (0, 1, 4, 9). Python has no tuple comprehension. The expression (n * n for n in range(4)) in parentheses is a generator, so wrap it in tuple() when you need a tuple.
Where the comma causes bugs
A comma you didn't mean to write still builds a tuple, and nothing warns you.
price = 9.99,
total = 1 + 2,
print(price, total)This prints (9.99,) (3,). Both variables hold one-element tuples, and a later price * 2 produces (9.99, 9.99) instead of 19.98. When a value has an unexpected type and no error was raised, look for a stray trailing comma.
Inside a function call, commas separate arguments instead of building a tuple. To pass a tuple as a single argument, you need a second pair of parentheses.
def show(x):
print(type(x).__name__, x)
show((1, 2))
show(1,)The first call prints tuple (1, 2). The second prints int 1, because the trailing comma in a call is ignored. Calling show(1, 2) would raise TypeError, since show takes one argument.
How Python decides
Quick reference
| Expression | Result type | Reason |
|---|---|---|
| `()` | tuple | Empty parentheses |
| `(5)` | int | Grouping only |
| `(5,)` | tuple | Trailing comma |
| `5,` | tuple | Comma, no parentheses |
| `tuple("ab")` | tuple | Converted from an iterable |
Exercise
What do a, b and c hold after a = (1, 2) + (3,), b = ("x") * 3 and c = ("x",) * 3?
Show answer
a is (1, 2, 3), because + joins two tuples into a new tuple. b is the string "xxx", because ("x") is only a string in grouping parentheses. c is ("x", "x", "x"), because the trailing comma makes a one-element tuple that * repeats.