[Tutor] Fwd: Difference between types

Albert-Jan Roskam fomcl at yahoo.com
Fri May 24 20:53:33 CEST 2013


<snip>

> A tuple is defined by commas, depending on context. However,
> parentheses are typically required because commas have low precedence.
> 
>     >>> 1, 2 + 3, 4
>     (1, 5, 4)
> 
>     >>> (1, 2) + (3, 4)
>     (1, 2, 3, 4)
> 
> An empty tuple is a special case:
> 
>     >>> x = ()
>     >>> type(x)
>     <type 'tuple'>

Why do I need to use a trailing comma to create a singleton tuple? Without a comma it seems to mean "parenthesized single object", ie the parentheses are basically not there.
>>> type((0,))
<type 'tuple'>
>>> type((0))
<type 'int'>
>>> (0)
0
>>> x = (,)
SyntaxError: invalid syntax

>>> (0,0)
(0, 0)



More information about the Tutor mailing list