Why does adding a trailing comma after an expression create a tuple?
Asked Answered
G

6

44

Why does adding a trailing comma after an expression create a tuple with the expression's value? E.g. in this code:

>>> abc = 'mystring',
>>> print(abc)
('mystring',)

Why is the printed output ('mystring',), and not just mystring?

Glazer answered 20/9, 2010 at 10:43 Comment(2)
see also: #7993059Rhizome
This is a great question teaching a basic fundamental idea in python.Vic
R
45

It is the commas, not the parentheses, which are significant. The Python tutorial says:

A tuple consists of a number of values separated by commas

Parentheses are used for disambiguation in other places where commas are used, for example, enabling you to nest or enter a tuple as part of an argument list.

See the Python Tutorial section on Tuples and Sequences

Residential answered 20/9, 2010 at 10:56 Comment(0)
M
12

Because this is the only way to write a tuple literal with one element. For list literals, the necessary brackets make the syntax unique, but because parantheses can also denote grouping, enclosing an expression in parentheses doesn't turn it into a tuple: you need a different syntactic element, in this case the comma.

Mystique answered 20/9, 2010 at 10:57 Comment(0)
U
6

Make sure to read this great answer by Ben James.


Tuples are not indicated by the parentheses. Any expression can be enclosed in parentheses, this is nothing special to tuples. It just happens that it is almost always necessary to use parentheses because it would otherwise be ambiguous, which is why the __str__ and __repr__ methods on a tuple will show them.

For instance:

abc = ('my', 'string')
abc = 'my', 'string'

What about single element tuples?

abc = ('mystring',)
abc = 'mystring',

So in effect what you were doing was to create a single element tuple as opposed to a string.

The documentation clearly says:

An expression list containing at least one comma yields a tuple. The length of the tuple is the number of expressions in the list. The expressions are evaluated from left to right.

Undertaker answered 20/9, 2010 at 10:51 Comment(3)
parentheses can be left out no matter how many elements tuple hasHearts
@SilentGhost: I caught myself and corrected it. Give me a moment before casting the stones, will you? :)Undertaker
Tuples are not indicated by the parentheses. Any expression can be enclosed in parentheses, this is nothing special to tuples. It just happens that it is almost always necessary to use parentheses because it would otherwise be ambiguous, which is why the __str__ and __repr__ methods on a tuple will show them.Residential
A
1

Unpacking multi-element tuple:

a, b = (12, 14)

print(type(a))

Output:

int

Unpacking single-element tuple:

a, = (12, )

print(type(a))

Output:

int

Otherwise:

a = (12,)

print(type(a))

Output:

tuple

Anana answered 26/9, 2014 at 20:37 Comment(1)
The question is not about how to work with the tuple, or what happens when the code runs. The question is about why the result is as it is. This answer is completely irrelevant.Amery
K
-1

In the question's example, you assigned the variable 'abc' to a Tuple with a length of 1.

You can do multiple assignments with this similar syntax:

x,y = 20,50

Also note that the print statement has a special understanding for ending a print statement with a comma; This tells print to omit the trailing newline.

print 'hello',
print 'world'

result:

hello world
Kania answered 2/8, 2014 at 1:7 Comment(1)
The question is about why a tuple is created, not about useful things that can be done with tuples. This answer is completely irrelevant. Also, Python 2.x's print statement did not actually handle tuples specially like that; the trailing comma on that line is part of the print statement syntax. You can easily verify this by adding parentheses to the intended tuple.Amery
M
-1

I was somewhat confused about the application of the comma, as you also apply a comma to make a list instead of tuple, but with a different variable assignment.

Hereby, a simple example that I made of how to create a tuple or a list.

abc = 1,2,3 # prints a tuple: (1, 2, 3)
*abc, = 1,2,3 # prints a list: [1, 2, 3]
Meingolda answered 29/9, 2020 at 20:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.