[Tutor] Variables (with lists??)

Andreas Perstinger andreas.perstinger at gmx.net
Wed Nov 23 06:56:15 CET 2011


On 2011-11-23 05:15, Chris Kavanagh wrote:
> I was going over one of Derek Banas' tutorials on youtube, and came
> across something I hadn't seen before. A variable with a list beside it
> (see code below). He sets the variable, customer , equal to a dict. Then
> uses the variable with ['firstname'],['lastname'], ect. I've never seen
> this in my short programming life. What is this called?

That's the Python syntax to indexing dictionary keys (comparable to the 
index of lists, tuples, ...)

In general, you set a value with

dictionary[key] = value

where "key" can be any immutable type (strings, numbers, tuples if they 
just contain strings, numbers and other tuples) and "value" anything you 
want to save for that key.

To get the value of a key, just use

dictionary[key]

Example:
 >>> customer = {}
 >>> customer["name"] = "Chris"
 >>> customer["name"]
'Chris'

> And is there any documentation on it??

The tutorial on dictionaries:
http://docs.python.org/tutorial/datastructures.html#dictionaries

The library reference on dictionaries:
http://docs.python.org/library/stdtypes.html#mapping-types-dict

Bye, Andreas


More information about the Tutor mailing list