Please provide a better explanation of tuples and dictionaries
John Gordon
gordon at panix.com
Wed Jan 30 00:15:43 EST 2013
In <hKCdnWgrOqkwFpXMnZ2dnUVZ_qadnZ2d at o1.com> "Daniel W. Rouse Jr." <dwrousejr at 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 at panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
More information about the Python-list
mailing list