Sunday, April 27, 2014

10. Python Dictionaries

Python Dictionaries are the Python's unordered data structure. They offer more powerful indexing options than integers, which are the only indexing option for Lists and Tuples. Like Lists, they are Mutable.




To create an empty Dictionary, we write a pair of brackets. We can initialize individual elements by putting them in brackets. Unlike Lists or Tuples, we need two objects for each element, one element is the index and the other element is the value. We can see extracting, as usual, is with the square brackets, and the len() function gives the length of elements in a Dictionary.




Once a Dictionary has been created, new elements are added by assignment statements. Notice the index is inside the square brackets, and the value is the term on the right hand side. Next, a Dictionary with three terms, is defined.




Besides being numbers such as integers, the index can be a String or a Tuple. Note an index must be immutable.




However, a List can not be used as an index, since a List is Mutable.




We can create a dictionary from two sequences. Here we have two Lists, A and B. The variables in A will be our keys and the variables in B will be our values. This is done with the zip() function. It creates a List of (Index,Value) Tuples. Finally if this List is operated by the dict() function, the Dictionary is created.




We can use a for statement to iterate over the keys. Here we use a List Comprehension to make a List of Indices in a Dictionary. This is similar to the keys() member function of Dictionaries.




Besides the keys() member function, we also have the values() function to return a List of values, and the items() function to return a List of index, value pairs.




We can use the in statement, to check if an index is in Dictionary. We may also use the get() function to get a value of a particular index. We might also give a default value. The default value is returned, if a particular index, is not found in the Dictionary. The default value is Python None object, and so nothing is returned for line 8, but in line 9, a 0 value is returned as we give a default value.




Like Lists, Dictionaries have a Comprehension statement. We have the value of each element in terms of the looping variable, all enclosed in brackets.




Now we have, a Dictionary example, which is similar to the example in the tutorial 8 on Python List Comprehensions. There, we had an example to get frequency for each of the 88 piano keys. The List created there had an index going from 0 to 87. It would be easier to have index terms like A0 and A4.




To analyze any problem, it is best to visualize in tabular format. For each index from 0 to 87, the note will have two parts: the name, given by the Pos term in this table and the Octave number, given by the Octave term in this table. The first note is A0. We only have 3 notes in Octave 0. Thus, we can see, that there is an offset of 9. This is the reason we have a 9 in the formulas for Pos and Octave. In Python 2.7, integer division results if both numerator and denominator are integers. The modulus operation gets the remainder.




First we have to define the 12 note names. Then we use a for loop to go over the entire 88 keys. We find the Octave and Pos variables according to the formulas we found earlier. In the print, string formatting, of %d (for integer) and %s (for String) are used. We covered this kind of printing earlier. The final comma, which is to the right of the closing parenthesis, tells the print to suppress normal printing of newline character. If we do not have this, we will have to scroll up and down since our list will not fit in one page.




The result of the print is shown here. It is usually good to have checks like this so our programs will not have errors which might keep on accumulating should there be no checks.




Finally we can create our Dictionary. Now instead of using the formatting flags, we may also use the str() function. This will convert most Python objects like integer to String. Notes is a List, but each element is a String already. Thus, now we can use index terms like 'A4'.




If we loop over some Dictionary with the in statement, we are looping over keys. Here for example, we find the number of black keys. The function count() will either return 0 or 1, since our string will have at most, one such symbol.




We can also test values of a Dictionary. For example, we may find the number of notes with frequencies between 100 and 300. This number is found to be 19. This number is between 12 (1 octave) and 24 (2 octave).




You will find additional information, including a larger image of the slides and text, at pythonaudio.blogspot.com.



This is the video for Tutorial 10:


1 comment:

About Me

I have used Python for the last 10+ years. I have a PhD in Electrical Engineering. I have taught Assembly Language programming of Intel-compatible chips as well as PC hardware interfacing. In my research, I have used Python to automate my calculations in physics and chemistry. I also use C++ and Java, often with Python.