[Tutor] (no subject)

Daniel Yoo dyoo@hkn.EECS.Berkeley.EDU
Fri, 1 Sep 2000 20:00:17 -0700 (PDT)


On Sat, 2 Sep 2000, deng wei wrote:

>    There is a mistake in the Python Manuals at "5.3 Tuples and
> Sequences".It said:
> 	a tuple with one item is constructed by following a value with a
> comma (it is not sufficient to enclose a single value in parentheses).
>
>    but if you type:
> >>> b=('hello')
> >>> b
> 'hello'


Parenthesis have two separate meanings in Python.  For example,
parenthesis are used to group stuff and lead Python to do certain things
first:

###
>>> 5 - (4 - 3)
4
>>> (5 - 4) - 3
-2
###

This shows how parenthesis can be used to indicate "precedence", that is,
what should be done first in an expression.  The other use for parenthesis
is in creating tuples.

###
>>> x = ('hello', 'world')
>>> x
('hello', 'world')
###

Usually, these two usages don't conflict, except for the case when you
want to make a tuple of one element.  Because

    x = (5 + (4 - 3))

can be taken both ways by someone reading the code, we need to make it
perfectly clear to Python what we mean.  That's why we have the "comma at
the end" rule:

###
>>> (5 - (4 - 3))
4
>>> (5 - (4 - 3),)
(4,)
###


Hope this clears things up.