Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Please provide a better explanation of tuples and dictionaries

Reply
Thread Tools

Re: Please provide a better explanation of tuples and dictionaries

 
 
John Gordon
Guest
Posts: n/a
 
      01-30-2013
In <(E-Mail Removed)> "Daniel W. Rouse Jr." <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed) B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dictionaries with tuples or tuples of tuples Jon Reyes Python 18 02-19-2013 03:56 AM
Re: Please provide a better explanation of tuples and dictionaries Rick Johnson Python 2 02-03-2013 07:21 AM
Re: Please provide a better explanation of tuples and dictionaries rusi Python 0 01-30-2013 08:14 AM
Re: Please provide a better explanation of tuples and dictionaries Mitya Sirenef Python 0 01-30-2013 03:51 AM
Re: Please provide a better explanation of tuples and dictionaries Chris Angelico Python 1 01-30-2013 03:51 AM



Advertisments