Learning Python 3 with the Linkbot/Dictionaries

This chapter is about dictionaries. Dictionaries have keys and values. The keys are used to find the values. Here is an example of a dictionary in use:

And here is my output:

1. Print Phone Numbers 2. Add a Phone Number 3. Remove a Phone Number 4. Lookup a Phone Number 5. Quit Type in a number (1-5): 2 Add Name and Number Name: Joe Number: 545-4464 Type in a number (1-5): 2 Add Name and Number Name: Jill Number: 979-4654 Type in a number (1-5): 2 Add Name and Number Name: Fred Number: 132-9874 Type in a number (1-5): 1 Telephone Numbers: Name: Jill    Number: 979-4654 Name: Joe     Number: 545-4464 Name: Fred    Number: 132-9874 Type in a number (1-5): 4 Lookup Number Name: Joe The number is 545-4464 Type in a number (1-5): 3 Remove Name and Number Name: Fred Type in a number (1-5): 1 Telephone Numbers: Name: Jill    Number: 979-4654 Name: Joe     Number: 545-4464 Type in a number (1-5): 5

This program is similar to the name list earlier in the chapter on lists. Here's how the program works. First the function  is defined. just prints a menu that is later used twice in the program. Next comes the funny looking line. All that this line does is to tell Python that  is a dictionary. The next few lines just make the menu work. The lines go through the dictionary and print all the information. The function  returns a list that is then used by the   loop. The list returned by  is not in any particular order so if you want it in alphabetic order it must be sorted. Similar to lists the statement  is used to access a specific member of the dictionary. Of course in this case  is a string. Next the line  adds a name and phone number to the dictionary. If  had already been in the dictionary   would replace whatever was there before. Next the lines see if a name is in the dictionary and remove it if it is. The operator  returns true if   is in   but otherwise returns false. The line  removes the key   and the value associated with that key. The lines check to see if the dictionary has a certain key and if it does prints out the number associated with it. Lastly if the menu choice is invalid it reprints the menu for your viewing pleasure.

A recap: Dictionaries have keys and values. Keys can be strings or numbers. Keys point to values. Values can be any type of variable (including lists or even dictionaries (those dictionaries or lists of course can contain dictionaries or lists themselves (scary right? :-) ))). Here is an example of using a list in a dictionary:

and here is a sample output:

1. Add student 2. Remove student 3. Print grades 4. Record grade 5. Print Menu 6. Exit Menu Choice (1-6): 3 hw ch 1        hw ch 2         quiz            hw ch 3         test Menu Choice (1-6): 5 1. Add student 2. Remove student 3. Print grades 4. Record grade 5. Print Menu 6. Exit Menu Choice (1-6): 1 Student to add: Bill Menu Choice (1-6): 4 Record Grade Student: Bill Type in the number of the grade to record Type a 0 (zero) to exit 1  hw ch 1     2   hw ch 2     3   quiz        4   hw ch 3     5   test 0              0               0               0               0  Change which Grade: 1 Grade: 25 Change which Grade: 2 Grade: 24 Change which Grade: 3 Grade: 45 Change which Grade: 4 Grade: 23 Change which Grade: 5 Grade: 95 Change which Grade: 0 Menu Choice (1-6): 3 hw ch 1        hw ch 2         quiz            hw ch 3         test Bill   25              24              45              23              95 Menu Choice (1-6): 6
 * 1) Max   25              25              50              25              100
 * 1) Max   25              25              50              25              100

Heres how the program works. Basically the variable  is a dictionary with the keys being the name of the students and the values being their grades. The first two lines just create two lists. The next line  creates a new dictionary with the key { } and the value is set to be  (since that's what   was when the assignment is made) (I use the key   since   is sorted ahead of any alphabetic characters). Next  is defined. Next the  function is defined in the lines:

Notice how first the keys are gotten out of the  dictionary with the   function in the line. is an iterable, and it is converted to list so all the functions for lists can be used on it. Next the keys are sorted in the line. is used to go through all the keys. The grades are stored as a list inside the dictionary so the assignment  gives   the list that is stored at the key. The function  just prints a list and is defined a few lines later.

The later lines of the program implement the various options of the menu. The line  adds a student to the key of their name. The notation  just creates a list of 0's that is the same length as the   list.

The remove student entry just deletes a student similar to the telephone book example. The record grades choice is a little more complex. The grades are retrieved in the line  gets a reference to the grades of the student. A grade is then recorded in the line. You may notice that  is never put back into the students dictionary (as in no  ). The reason for the missing statement is that  is actually another name for   and so changing   changes.

Dictionaries provide an easy way to link keys to values. This can be used to easily keep track of data that is attached to various keys.

Using a Dictionary to Store Musical Notes
Up to this point, our Linkbot programs have been playing musical notes on the Linkbot either by specifying a specific frequency to play, such as, or using an equation to calculate the frequency based on a keyboard key number. Wouldn't it be nice if we could just tell the Linkbot to play notes like "Do-Re-Mi", or "A-B-C"?

Perhaps we can use a dictionary to accomplish this task. We can use a dictionary that uses the strings "Do", "Re", "Mi", etc. as keys, and their actual frequencies as values. To find the actual values, we can use an equation or simply look them up online. A quick search shows these frequencies:

Now we can create and use a dictionary to play notes.


 * 1) Here, we define a helper function that helps us play notes. The function takes a linkbot object and a string note value such as "Do" as input arguments and plays that note on the linkbot for 0.5 seconds.
 * 2) This is where we create the dictionary that relates note names to note frequencies.
 * 3) This list defines the notes in a simple song we want to play.
 * 4) This loop takes each note in the list of notes that we constructed earlier and plays it using our   function.