I want to know that why adding a trailing comma after a variable name (in this case a string) makes it a
'mystring', print(abc) ('mystring',)abc =
When I print
abc it returns the
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.
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.
See above for a much better answer.
In python a tuple is indicated by parenthesis.
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
__repr__methods on a tuple will show them.
I stand corrected (all I’ve been doing today. Sigh).
abc = ('my', 'string')
What about single element tuples? The parenthesis notation still holds.
abc = ('mystring',)
For all tuples, the parenthesis can be left out but the comma needs to be left in.
abc = 'mystring', # ('mystring',)
abc = 'my', 'string', # ('my', 'string',)
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.
Unpacking multi-element tuple:
a, b = (12, 14) print type(a)
Unpacking single-element tuple:
a, = (12, ) print type(a)
a = (12,) print type(a)
When you see a comma after a single value, that value is interpreted as the datatype ‘tuple’.
Here is a little something I’ve learned through experience that may apply to some of you:
If you’re a musician, the word tuple may be confusing, since the words tuple and triple are used to describe groupings of notes that are used within a certain type of time signature that they are not strictly compatible with. For example a grouping of two eighth notes played as if the time signature were 4/4 (straight feel) when the time signature is 6/8 (triplet feel). Or vice versa a triplet played in 4/4 time. This leads the novice programmer to perhaps interpret a tuple as a pair of values.
This isn’t the same kind of tuple as you see in programming. These tuples are an immutable (un-alterable once assigned) sequence data type that can hold any number of values but can be considered to be transferred together as if they were all enclosed between to parentheses, or in other words, a tuple of parentheses.
You can’t add or delete stuff from a tuple once it is assigned, so it is usually used to pack and unpack variables. I use it frequently to return multiple values from a function:
def somefunction_foo(some_data_file): map1 = dict() map2 = dict() map3 = dict() with open(datafile, 'r') as file: # auto-close the file after this block for row in file: pass # don't actually pass, but # fill each map with specific data from the same file return map1, map2, map3 # I'm returning a tuple, but without parenthesis
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'
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]