Wednesday, April 23, 2014

8. Python List Comprehensions

Python List Comprehensions are a quick way of initializing Lists of data.




A simple example is to create a List of the squares for numbers 1 through 10. For most List operations, the function range() is very important.




We can get documentation for the range() function. It states that there are two ways of using this function depending on the number of arguments. The first one, range(stop) will generate a list from 0, 1, 2, and so on, to stop minus 1. We can use the second way to specify a different start or step, rather than the default values of 0 for start and 1 for step. If we were to use Python 3, we will have to apply the list() function, to the value returned by the range() function, to have the same functionality.





We can see what kinds of values are returned by range(1), range(2), range(3) and range(4). Thus, we can see that range(4) returns 4 integers from 0 to 3.




For the range() function with the start argument, we first start the range at value of 1, and then we apply a step of 2.




A for loop is used to iterate over the range of integers from 1 to 10, and each time add an element to the List. The value of the element is square of the looping variable.




This shows an example of List Comprehension to do the same thing. We can put the for loop inside of the square brackets. The value comes first and the for loop comes second.




A better List Comprehension would have a range(10) function and change the value to reflect we want to start at 1 square and not 0 square.




A List Comprehension can have an if condition after the for loop. This will filter out the i = 9 case.




This is another example of a List. This List will have a length of 88, from 0 to 87. The first component will have a value of 27.5. The last will be about 4186.01.




The frequency relation is that after every 12 keys, the frequency is doubled. We can see what the values of the list from 0th, 12th, 24th, 36th, and 48th index are. Remember the index numbers start with 0.




If 2 is raised to the power of 8, that is 2 is multiplied by itself 8 times, we will get 256. We can find the 8th root of 256 by writing 256 to power of one-eighth. In Python, the exponent function is two multiplication symbols.




We can calculate what frequency ratio is one semitone, the frequency ratio between any two nearest keys.




This is the way to generate the entire 88 keys. We are only given the lowest frequency (A0) at 27.5 Hz. Next key is one semitone higher, and so on. Now we can find any key. For example the 49th key, or the 48th index, is 440 Hz.




We can combine the last relationship to get this List Comprehension.




Now we can print out the A note frequencies for the eight octaves. It should be seen that each comma, inside the print command, inserts a space.




If we want greater control over printing, we have to use the formatting flags: the %d flag is to print an integer, the %f flag, is to print a floating-point number. For % period 1f, we tell Python that it should only print 1 digit after the decimal point. The quoted expression is separated from the values by a % symbol. The values are stored in a structure called tuple. We will cover tuples in next tutorial.




You may go to pythonaudio.blogspot.com to see the slides. To see a larger image of the slide, you can click on them at that page, which provides easy navigation controls. The text for the audio of the slides as well as any relevant source code is also on that page.



This is the video of Tutorial 8:


No comments:

Post a 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.