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]
p[2] # Remember zero-based indexing
5
(r != 20) and (r * 100 < 200)
s.count('o') # count() returns the number of times its argument occurs in the objectp + [r]
s[0:4]
down
p[4:]
[11, 13]
2. For each of these sequences of statements, what does Python print?
p = [2, 4, 6, 8]
print(p[0] + p[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)
3. The Anteater Grocery Store represents each item in its inventory with:
Define a namedtuple called Item to represent grocery items as described above.
Item = namedtuple('Item', 'name price in_stock')
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.
item1 = Item("Campbell's Chicken Soup", 1.25, 250)
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).
item1.price * item1.in_stock
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.
L[0].price * L[0].in_stock + L[-1].price * L[-1].in_stock