[Tutor] Dictionaries of Tuples of Strings

Bill Sconce sconce at in-spec-inc.com
Fri Feb 23 23:17:48 CET 2007


On Fri, 23 Feb 2007 13:45:54 -0800 (PST)
Terry Carroll <carroll at tjc.com> wrote:

> > Any other way to create an empty tuple? 
> 
> Answering my own question:
> 
> >>> t=()
> >>> type(t)
> <type 'tuple'>


Giving the lie to my earlier summary (that tuples are
indicated by commas only -- arrgh  :)
______________________________________________________________________
All right, then, let's get it right.  From THE tutorial (in the kit):
    A tuple consists of a number of values separated by commas,
    for instance:
        >>> t = 12345, 54321, 'hello!'
        >>> t[0]
        12345
        >>> t
        (12345, 54321, 'hello!')
    ...As you see, on output tuples are always enclosed in parentheses,
    so that nested tuples are interpreted correctly; they may be input
    with or without surrounding parentheses, although often parentheses
    are necessary anyway (if the tuple is part of a larger expression).
    
    ...A special problem is the construction of tuples containing 0 
    or 1 items: the syntax has some extra quirks to accommodate these.
    Empty tuples are constructed by an empty pair of parentheses; 
    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). Ugly, but effective.
                  ^^^^
            in the original :)

    >>> empty = ()
    >>> singleton = 'hello',    # <-- note trailing comma
    >>> len(empty)
    0
    >>> len(singleton)
    1
    >>> singleton
    ('hello',)
    
 
 
HTH,

-Bill


More information about the Tutor mailing list