[Tutor] Fwd: Difference between types
Albert-Jan Roskam
fomcl at yahoo.com
Sat May 25 14:54:18 CEST 2013
> From: Dave Angel <davea at davea.name>
>To: tutor at python.org
>Sent: Friday, May 24, 2013 9:10 PM
>Subject: Re: [Tutor] Fwd: Difference between types
>
>
>On 05/24/2013 02:53 PM, Albert-Jan Roskam wrote:
>> <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)
>>
>
>Your answer is right above your question, in the part you quoted
>(without attribution) from eryksun.
>
>The empty tuple is specified with (). But for any tuple with one or
>more members, it's the commas that turn it into a tuple. The parens are
>not necessarily needed unless the statement is complex enough that we
>need them for precedence.
>
>So x = 3,4
>
>makes a one-tuple out of 3 and 4. If you want a one-tuple (which is NOT
>a singleton), you need a silly-looking comma to specify it:
So you say the term singleton is reserved to one-item sets? I looked it up and: "In mathematics, a singleton, also known as a unit set, is a set with exactly one element. For example, the set {0} is a singleton.The term is also used for a 1-tuple (a sequence with one element)".(http://en.wikipedia.org/wiki/Singleton_%28mathematics%29).
<snip>
More information about the Tutor
mailing list