What can you do with a dictionary?

The full list of methods is in Section 2.2.7 of the Python Library Reference. Here's an idea of how you might use and combine these methods to carry out common operations.

Create a dictionary,

Access an element,

The general pattern here is dictionary_name[ key ] = value. So to modify a value,

and to insert a new value,

Check to see if there is already an entry with a particular key,

or alternatively,

Delete an entry in the dictionary (both the key and its associated value),

Remove an entry, but get the value,

Find out how many key-value pairs are in the dictionary,

Get a list of all the keys in the dictionary,

>>> d.keys()
['Tim', 'Doug']
>>>

(Note the square brackets and commas: d.keys returns a list.)

This is often used for iterating through the entries in the dictionary, e.g.

for key in d.keys():
    print key, ':', d[key]

produces the output,

>>> 
Tim : 820
Doug : 870
>>> 

In recent versions of Python this can be shortened to,

for key in d:
    print key, ':', d[key]

Get a list of all the values that occur in a dictionary,

>>> d.values()
[820, 870]
>>> 

Delete all the entries in the dictionary,

>>> d.clear()
>>> d
{}
>>> 

Note that the name d still exists; it just references (points at) an empty dictionary.

Finally note that the keys don't have to be strings they can be any immutable type, and the values can be any Python object, e.g.

>>> d['Tim'] = 775
>>> d[3] = ['a', 'small', 'list']
>>> d[(0,1)] = 'X'
>>> d
{(0, 1): 'X', 'Tim': 775, 3: ['a', 'small', 'list']}
>>> for key in d:
    print key, ':', d[key]
(0, 1) : X
Tim : 775
3 : ['a', 'small', 'list']
>>> 

* Literals are how to write out values.