Final review questions, CMPT 383 Fall 2010
Consider the attached Python listing (on the last page). You are encouraged to run the examples to
understand how they work.
1. Function example1 executes function doSomething() on every element in list
container. Change the function to execute doSomething() on every element in
container, assuming container is a Python set instead of a list. How are the two
versions similar? How do they differ? What is the name for this property?
2. Consider function example2. What are the scopes of variables a, b, and c? Which values, if
any, will persist after the function returns?
3. Consider the sequence of code Example 3. What is printed by the two print statements? Why?
4. Consider the sequence of code Example 4. Assume that list a and tuple b have the
representations in memory I discussed in class.
• Which variable should permit faster indexing (i.e., would a or b be faster
or would they both be the same speed)?
• How expensive would the conversion from list form to tuple form be? Would you want
to do that in a tight loop?
• What are the advantages of the internal representation for a list over the internal
representation for a tuple?
5. Consider the sequence of code Example 5. At the point of the statement b = a, how many
bindings exist for the name a?
6. Consider a (vastly simplified) mapping application that converts locations to business street
addresses and vice versa. Assume locations are represented by a (latitude, longitude) tuple,
with latitude and longitude each represented by a tuple of two numbers and an address is a
string, such as “8888 University Drive”.
• Create a dictionary that converts 49.279, 122.920 to “8888 University Drive” and 0,0 to
• Write a for loop that prints every (location, address) pair in the dictionary
• Write a routine that, given an address, returns its location in the above dictionary or
(None, None) if the address is not in the dictionary.
1. Describe the difference between mutable and immutable types in Pytho