ICS 31 -- Winter 2013 -- Quiz 2

  • 1. What is the value of each of the Python expressions below? Use these values where appropriate:

    r = 17
    s = 'download'
    p = [2, 3, 5, 7, 11, 13]
    1. p[2] # Remember zero-based indexing

      Answer Feedback:
      5

      You should also be able to evaluate expressions like
      p[1:4], p[1:], p[-4], and p[-4:]
    2. (r != 20) and (r * 100 < 200)
      Answer Feedback:
      False
    3. s.count('o')  # count() returns the number of times its argument occurs in the object
      Answer Feedback:
      2
    4. p + [r]
      Answer Feedback:
      [2, 3, 5, 7, 11, 13, 17]
      The square brackets and the commas are
      important here; they're part of the Python syntax
      for lists.
    5. s[0:4]
      Answer Feedback:
      down

      When taking "slices" of strings or lists, the value after the colon is not the position of the ending character or list element (i.e., the answer here is not "downl", ending at s[4]). Instead, it's the position JUST BEFORE WHICH the slice ends (giving us the correct answer, s[0] through s[3] or 'down'). There's a reason for this skewed-seeming behavior: It lets us use a value like len(s) to the right of the colon without having to subtract 1 from it.
    6. p[4:]
      Answer Feedback:
      [11, 13]

      Start at position number 4, go to the end. (If you answered [11, 13, 17], you have the right idea, but the expression p + [r] doesn't change the value of the list p---it's not an assignment statement or a method that changes its object's value. Also, separate parts of a quiz question like this are generally treated independently, rather than as a sequence of expressions that affect each other in order.)
  • 2. For each of these sequences of statements, what does Python print?

    1. p = [2, 4, 6, 8]
      print(p[0] + p[2])
      Answer Feedback:
      8
    2. Restaurant = namedtuple('Restaurant', 'name cuisine phone dish price')
      fancy = Restaurant('Taillevent', 'French', '01-11-22-33-44', 'Loup de Mer', 55.00)
      fast = Restaurant("McDonald's", 'Burgers', '334-4433', 'Big Mac', 3.95)
      print(fast.name, 'serves', fast.cuisine)
      print('True or False:', fancy.price > fast.price)
      Answer Feedback:
      McDonald's serves Burgers
      True or False: True
  • 3. The Anteater Grocery Store represents each item in its inventory with:

    • a string representing the item's name, e.g., 'Granny Smith Apples 1 lb.'
    • a float representing the item's price, e.g., 2.50
    • an int representing how many of this item are in stock, e.g., 85

    1. Define a namedtuple called Item to represent grocery items as described above.

      Answer Feedback:
      Item = namedtuple('Item', 'name price in_stock')

      (The names of the fields---name, price, and in_stock---could be different, so long as you use them consistently below. For quiz purposes the import statement isn't required, since we didn't ask for it, but it wouldn't be wrong to include it here.)

    2. Write a statement that assigns to the variable item1 an Item representing Campbell's Chicken Soup, selling for $1.25 per can, with 250 cans in stock.

      Answer Feedback:
      item1 = Item("Campbell's Chicken Soup", 1.25, 250)

      (Make sure the order of the arguments to the Item constructor is as shown.
      Even if the problem had said "Campbell's Chicken Soup, with 250 cans in stock selling for $1.25 per can," Item must be called with the field values in the order shown above and specified when we called namedtuple.)

    3. Write a Python expression for the value of the store's inventory of item1 (that is, how much money we'd take in if we sold all of that item we have in stock).  

      Answer Feedback:
      item1.price * item1.in_stock

      (The problem just asked for an expression, as above; it didn't ask for an assignment statement or a print statement. If your included print or assignment, it's possible you wouldn't lose points if the correct expression were also included, but it's always best to provide precisely what the problem asks for.)

    4. Suppose we have this list of items:


      L = [Item('oranges', 2.50, 20),
      Item('plums', 3.25, 40),
      Item('pears', 3.00, 35),
      Item('peaches', 2.50, 40) ]


      Write a Python expression representing the total value of the inventory of the first and last items on the list. For full credit, your expression should work for a list of any length greater than 1.

      Answer Feedback:
      L[0].price * L[0].in_stock + L[-1].price * L[-1].in_stock