David Abel

Overview

0. Instructions

1. Basics

2. Functions

3. Dictionaries

4. OOP

5. Modules

6. Debugging

7. Tips & Tricks

Python Tutorial

python logo

This tutorial is intended as an introduction to Python for folks with some prior programming experience.

Section 3: Dictionaries (dictionaries.py)

Here we will play with another common data strucutre: the hash map. In python, they are called "dictionaries", and are defined with curly braces:

>>> new_dict = {}

You can also initialize a dictionary with some key-value pairs as follows:

>>> new_dict = {"Oregon" : 5, "California" : 13}

To access a value in the dictionary for a given key, use:

>>> print new_dict["Oregon"]
5

To create a new key-value pair, use:

>>> new_dict["Texas"] = 30
>>> print new_dict["Texas"]
30

To grab the list of keys, or the list the values, use:

>>> print new_dict.keys()
["Oregon", "California", "Texas"]
>>> print new_dict.values()
[5, 13, 30]

This allows you to loop through dictionaries as follows:

>>> for key in new_dict.keys():
>>>     print new_dict[key]
5
13
30

Open up (dictionaries.py). Here we are going to implement a function that will count the number of palindromes of each length in Alice in Wonderland (alice.txt).

Implement the function count_palindromes_of_each_length(filename). The File I/O to read in Alice in Wonderland has already been included in the main function (feel free to take a look if you're curious).

To do this, you will want access to your palindrome checker function from the previous section. We can import is using the import keyword:

>>> import basics

This will give us access to the entire scope of the basics file we just wrote in the previous section. To call the is_palindrome method, execute:

>>> print basics.is_palindrome("amanaplanacanalpanama")
True

Alternatively, it is often useful to only import a particular function. We can do this as follows:

>>> from basics import is_palindrome
>>> print is_palindrome("amanaplanacanalpanama")
True

One extremely handy extension to the standard python dictionary is the defaultdict(type) data-type. To access it, we'll need to import it from the collections module:

from collections import defaultdict

The defaultdict(type) datastructure takes as input a type (int, str, list, etc), and will automatically define the value of any queried key not yet defined to be the 0 equivalent of that data type. For instance:

>>> from collections import defaultdict
>>> str_dict = defaultdict(str)
>>> print str_dict[55]
""
>>> int_dict = defaultdict(int)
>>> print int_dict["Colorado"]
0
>>> int_dict["Colorado"] += 5
>>> print int_dict["Colorado"]
5
>>> list_dict = defaultdict(list)
>>> print list_dict[503]
[]

You can probably imagine why this might be useful (the alternative behavior is a KeyError!). That should be all you need to count palindromes, happy coding! If everything works properly, there should be 931 (uninteresting) palindromes of length 1, and 56 palindromes of length 3.

< Previous Section Next Section >