Saturday, April 26, 2014

9. Python Tuples

Python Tuples are the primary Python data structure which are ordered and immutable. Immutable means that the values of the different indices can not be reassigned once their values have been defined.




An empty Tuple is just a pair of parenthesis, while a one-element Tuple has the value with a comma at end, inside the parenthesis.




Tuples do not have to have parenthesis around them. Usually whenever you see a list of numbers, it is treated as a Tuple. This will be seen to be important in both sending values to external functions as well as getting values from the functions, as we will later see.




If you have the same number of elements before and after an assignment, the values will be unpacked to individual variables. Thus here we define the variable a to be 1, and the variable b to be 2.




Unlike Lists, we can not use the append() function to add values. However we can use the plus operator to join tuples.




This is the immutable property of a Tuple. The individual indices can't be changed once we have given them values. We can see that we again use the square brackets to extract an element, which is common in Python.




Even though a Tuple can not be changed element-wise, we can make global changes. This is due to the fact that in Python, variable names are only pointers to the actual data. If they lose reference, Python automatically deletes them.




We can use the list() function to change a Tuple to a List. A List is Mutable and individual elements can be changed.




Once an element has been changed, we can turn it back into a Tuple with the tuple() function.




Likewise, if we use, the function tuple() on a string, its characters are placed in individual indices.




As before, we can test membership of an element with the in command. Tuples have count() and index() functions with similar functionality as their List counterparts.




We can use a for statement to loop the elements in a Tuple. For example, we can find out how many vowels are in a Tuple with this example.




Unlike List Comprehensions, there are no Tuple Comprehensions. However if we use parenthesis with a comprehension loop, it generates a Python generator object. Basically a generator object knows how to generate itself, on demand, but does not store all values saving memory. We can use the tuple() function to force it to generate all values at once and store the result as a Tuple. These two steps can be considered as constructing a Tuple.




Tuples will return the number of elements with the len() function.




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 9:


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:


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.