X-Received: by 10.224.190.193 with SMTP id dj1mr1958141qab.6.1359522944236; Tue, 29 Jan 2013 21:15:44 -0800 (PST) MIME-Version: 1.0 Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!p13no8704487qai.0!news-out.google.com!k2ni4023qap.0!nntp.google.com!newsfeed2.dallas1.level3.net!news.level3.com!panix!gordon From: John Gordon Newsgroups: comp.lang.python Subject: Re: Please provide a better explanation of tuples and dictionaries Date: Wed, 30 Jan 2013 05:15:43 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Lines: 45 Message-ID: References: NNTP-Posting-Host: panix2.panix.com X-Trace: reader1.panix.com 1359522943 1918 166.84.1.2 (30 Jan 2013 05:15:43 GMT) X-Complaints-To: abuse@panix.com NNTP-Posting-Date: Wed, 30 Jan 2013 05:15:43 +0000 (UTC) User-Agent: nn/6.7.3 Xref: csiph.com comp.lang.python:37922 In "Daniel W. Rouse Jr." 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"