[Python Basic] What does (1,) means? — Tuples

A Ydobon
4 min readJun 22, 2019
Photo by Anton Belashov on Unsplash

Consider the following code example.

We might expect some errors to be displayed because a = 1,000,000 doesn’t seem to be the right expression. Or we might expect 1,000,000 to be printed. The output is:

Definitely we provide 1,000,000 to the variable named a. When printing, we get a tuple of (1, 0, 0). Hmm…

To better understand what happened, let us recall the usual explanation for tuples. Tuples are almost identical to lists, except they are immutable. Since immutability is the most important aspect of tuples, sometimes we forget how to define tuples.

Tuples may be constructed in a number of ways:

  • Using a pair of parentheses to denote the empty tuple: ()
  • Using a trailing comma for a singleton tuple: a, or (a,)
  • Separating items with commas: a, b, c or (a, b, c)
  • Using the tuple() built-in: tuple() or tuple(iterable)

Since all of the 4 different ways tell us to use parenthesis, we are easily misguided to believe that what makes tuples is the parenthesis. But this is not true.

It is actually the comma that makes tuples, not the parentheses. The parentheses are optional, except in the empty tuple case, or when they are needed to avoid syntactic ambiguity. For example, f(a, b, c) is a function call with three arguments, while f((a, b, c)) is a function call with a 3-tuple as the sole argument.

Now, look at the example again.

1,000,000 is not interpreted as one million, but as 1, 000, 000 (note the blanks between numbers) three numbers with separating commas. It is exactly the way to construct a tuple. So, the print statement shows the tuple of (1, 0, 0).

Remember that

It is actually the comma which makes tuples, not the parentheses.

Now we need some practices.

  1. To make an empty tuple,
a = ()

2. To make a tuple with only one element,

a = (1,)
a = 1,

3. To make a tuple with two or more elements

a = 1, 2, 3
a = (1, 2, 3)

4. Using built-in tuple() method,

a = tuple()
a = tuple([1, 2, 3])

--

--