Lecture 6: Nested Sequences

Topics

Piazza

  • Ask questions on Piazza!

movies.py

  • tuples are useful for representing data with fixed entries
    • look at the movie_scores variable declaration
      • a list of tuples (specifically, pairs)
        • remember, we can store any value in a list, including tuples
  • write a function called print_movies that takes in the a list of tuples (like movie_scores) and prints each movie followed by its score, one movie per line
  • look at the print_movies function in movies.py
    • iterates over the list, just like any other list
    • movie_pair is a tuple (each entry in the list is a tuple)
      • we unpack the tuple to get at the two values in the tuple
        • we also could have written movie_pair[0] and movie_pair[1] (see print_movies2) though I think using unpacking is much cleaner
    • once we have the two values, we can print them out
      • \t is a special character that represents a tab (like \n, which represents the end of line character)
  • look at the print_movies3 function in movies.py
    • we can unpack the two values of the tuple in the for loop
    • any of the variants is fine for this class
  • what does the get_movie_score function do in movies.py?
    • takes two parameters, a movie title and the movie information
    • iterates through the movies and tries to find the matching title
      • if it finds it, it returns the score
      • if it doesn't find it, it will iterate through all of the movie entries, finish the for loop and return -1
  • write a function called my_max that takes a list of positive numbers in and returns the largest one

    def my_max(numbers):
       max = -1
    
       for num in numbers:
          if num > max:
             max = num
    
       return max
    
    • key idea: have a variable that keeps track of the largest number seen so far
      • at each iteration, compare the current number to max, if it's bigger, update the max value
    • why initialize it to -1?
      • need to initialize it to something that is smaller than any of the values
      • could also have done something like max = numbers[0] (assuming that the input would have at least one value)
  • look at the get_hightest_rated_movie function in movies.py
    • very similar idea to my_max function
      • we're finding the largest score
      • also keep track of the movie with the highest score so that we can return that at the end
  • write a function called get_movies_above_threshold that takes as input a the movie scores and a score threshold and returns all of the movies above that threshold
    • Hint: it should look a lot like some of the other examples that we've seen that build up a list of answers (e.g. string_to_list in more_lists.py)
  • look at get_movies_above_threshold function in movies.py
    • start out with an empty list
    • iterate through the movie collection
    • check each movie score
      • if the score greater than or equal to the threshold, append it to the end of our list of movies that are above the threshold

Iterating over multiple lists

  • Sometimes you can't get away with for elt in list
    • What if you have two lists?
    • for e1, e2 in zip(list1, list2) is one option
    • But sometimes the indexing is complicated
  • You can use for i in range and use the same i for both lists!

= (i.e. assignment)

  • when we say x = y what we're actually saying is "let x reference the same thing that y references"
  • x and y are still separate variables
    • if we say x = z that does NOT change the value of y
  • references
    • objects reside in memory
    • anytime we create a new int, float, string, list, etc. it is allocated in memory
    • variables are references to objects in memory
      • a variable does NOT hold the value itself, but it is a references to where that value resides
      • arm/hand metaphor
        • think of variables like your hands
        • you can point them at things and then ask questions:
          • what is the name of the thing that my right hand points to?
          • for the thing that my left hand points to, do something?
        • you can point them to new things (i.e. assignment)
        • you can point point them at the same thing (i.e. assignment one to the other)
        • but they are separate things!
          • similarly, variables are separate things
          • they can reference the same thing, but they are not the same thing
    • when you ask for the value of a variable, it's actually getting the value of the things it references in memory

mutable objects

  • if an object is mutable (i.e. it has methods that change the object) then we have to be careful when we have multiple things referencing the same object (this is called "aliasing")

    >>> x = [1, 2, 3, 4, 5]
    >>> y = x
    
    • what does this look like in memory?
      • there is one list object
      • both x and y are references to that same object (drawn as arrows in class)
    • what happens when I call a method that changes/mutates the object?

      >>> y.reverse()
      >>> y
      [5, 4, 3, 2, 1]
      >>> x
      [5, 4, 3, 2, 1]
      
      # x and y are references to the same object!
      
      >>> x[0] = 0
      >>> x
      [0, 4, 3, 2, 1]
      >>> y
      [0, 4, 3, 2, 1]
      
      # statements that mutate the object that are done on either variable will affect this object
      
      >>> y[0] = 15
      >>> x
      [15, 4, 3, 2, 1]
      >>> y
      [15, 4, 3, 2, 1]
      
    • If we change what one of them references using assignment, then it will NOT affect the original object

      >>> y = [0] * 5
      >>> y
      [0, 0, 0, 0, 0]
      >>> x
      [0, 4, 3, 2, 1]
      
      • there are now two separate objects and x and y each reference a different object
    • why hasn't this problem come up with ints/floats/bools/strings? We said they're objects?

      >>> x = 10
      >>> y = x
      
      • what does the memory picture look like?
        • we just have one int object!
        • x and y are both references to that one int object
      • it seems like we could have the same problem as with lists…
      • ints/floats/bools/strings are not mutable!
        • there is no way for us to change the underlying object
          • therefore, even though two variables reference the same int, it's as if they referenced separate ints
      • draw the memory picture for the following:

        >>> x = 10
        >>> y = x
        >>> y = 15
        >>> x
        10
        >>> y
        15
        
        • we now have 2 objects (10 and 15)
        • just like with lists, changing what y references does not affect x

Author: Joseph C. Osborn

Created: 2020-04-21 Tue 10:44

Validate