Practice Problems 7 For the week of 2020-04-06

The following exercises provide additional practice problems (not to turn in) for our material this week. Try to solve each problem on paper first before using Thonny to confirm your answers. You are encouraged to work on these practice exercises with classmates throughout the week.

  1. Implement two different approaches to add 5 to the end of the list [1, 2, 3, 4], one that modifies the original list and the other that does not.

  2. [PP Problem 8.9] Draw the memory model showing the effect of the following statements (like the figure that would be produced by pythontutor.com):

     values = [0, 1, 2]
     values[1] = values
    
  3. What is the value of the list a after this code executes?

     def mystery(a_list):
         a_list.sort()
         return a_list[0]
        
     a = [3, 7, 2, 9, 1]
     print("Result:", mystery(a))
    
  4. What is the value of the list a after this code executes?

     def mystery(a_list):
         a_list = sorted(a_list)
         return a_list[0]
        
     a = [3, 7, 2, 9, 1]
     print("Result:", mystery(a))
    
  5. What is the value of the list a after this code executes?

     def mystery(a_list):
         a_list = a_list[::]
         a_list.sort()
         a_list[1].append(2)
         return a_list[0][0]
        
     a = [[3], [7], [2], [9], [1]]
     print("Result:", mystery(a))
    
  6. Draw the Python memory model after this code executed (like the figure that would be produced by pythontutor.com):

     a = [1, 2, [3, 4], 5]
     b = a[:]
     a[2].append(4)