Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #37922

Re: Please provide a better explanation of tuples and dictionaries

From John Gordon <gordon@panix.com>
Newsgroups comp.lang.python
Subject Re: Please provide a better explanation of tuples and dictionaries
Date 2013-01-30 05:15 +0000
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <keaa9v$1ru$1@reader1.panix.com> (permalink)
References <hKCdnWgrOqkwFpXMnZ2dnUVZ_qadnZ2d@o1.com>

Show all headers | View raw


In <hKCdnWgrOqkwFpXMnZ2dnUVZ_qadnZ2d@o1.com> "Daniel W. Rouse Jr." <dwrousejr@nethere.comNOSPAM> writes:

> I have recently started learning Python (2.7.3) but need a better 
> explanation of how to use tuples and dictionaries.

A tuple is a linear sequence of items, accessed via subscripts that start
at zero.

Tuples are read-only; items cannot be added, removed, nor replaced.

Items in a tuple need not be the same type.

Example:

    >>> my_tuple = (1, 5, 'hello', 9.9999)
    >>> print my_tuple[0]
    1
    >>> print my_tuple[2]
    hello

A dictionary is a mapping type; it allows you to access items via a
meaningful name (usually a string.)

Dictionaries do not preserve the order in which items are created (but
there is a class in newer python versions, collections.OrderedDict, which
does preserve order.)

Example:

    >>> person = {} # start with an empty dictionary
    >>> person['name'] = 'John'
    >>> person['age'] = 40
    >>> person['occupation'] = 'Programmer'
    >>> print person['age']
    40

Dictionaries can also be created with some initial values, like so:

    >>> person = { 'name': 'John', 'age': 40, 'occupation' : 'Programmer' }

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Please provide a better explanation of tuples and dictionaries "Daniel W. Rouse Jr." <dwrousejr@nethere.comNOSPAM> - 2013-01-29 18:55 -0800
  Re: Please provide a better explanation of tuples and dictionaries Chris Angelico <rosuav@gmail.com> - 2013-01-30 14:11 +1100
    Re: Please provide a better explanation of tuples and dictionaries "Daniel W. Rouse Jr." <dwrousejr@nethere.comNOSPAM> - 2013-01-29 19:42 -0800
      Re: Please provide a better explanation of tuples and dictionaries Chris Angelico <rosuav@gmail.com> - 2013-01-30 14:51 +1100
  Re: Please provide a better explanation of tuples and dictionaries Mitya Sirenef <msirenef@lightbird.net> - 2013-01-29 22:51 -0500
  Re: Please provide a better explanation of tuples and dictionaries John Gordon <gordon@panix.com> - 2013-01-30 05:15 +0000
    Re: Please provide a better explanation of tuples and dictionaries "Daniel W. Rouse Jr." <dwrousejr@nethere.comNOSPAM> - 2013-01-29 22:14 -0800
      Re: Please provide a better explanation of tuples and dictionaries Chris Angelico <rosuav@gmail.com> - 2013-01-30 17:25 +1100
      Re: Please provide a better explanation of tuples and dictionaries Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-30 07:12 +0000
  Re: Please provide a better explanation of tuples and dictionaries rusi <rustompmody@gmail.com> - 2013-01-30 00:14 -0800
  Re: Please provide a better explanation of tuples and dictionaries Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-02 21:20 -0800
    Re: Please provide a better explanation of tuples and dictionaries Michael Torrie <torriem@gmail.com> - 2013-02-03 00:14 -0700
    Re: Please provide a better explanation of tuples and dictionaries Chris Angelico <rosuav@gmail.com> - 2013-02-03 18:21 +1100

csiph-web