4.14. Midterm

Info:

Name:
Red ID:

For the questions below, assume all of the the following assignment statements have been executed:

 orders = {'coop': {'tea': ('black', 'red'),
                    'coffee': ('decaf','caf')},
          'bogie': {'tea': ('red', 'red', 'red'),
                    'ham_sandwich': ('med', 'well', 'med'),
                  'fritata': ('sausage', 'plain', 'bacon')},
          'grant': {'tiramisu': ('pumpernickel',),
                    'tea': ('black', 'red', 'black'),
                    'french_toast': ('sunny_side_up', 'over_easy')}}


  squares = ['A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9',
             'B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B9',
             'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9',
             'D1', 'D2', 'D3', 'D4', 'D5', 'D6', 'D7', 'D8', 'D9',
             'E1', 'E2', 'E3', 'E4', 'E5', 'E6', 'E7', 'E8', 'E9',
             'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9',
             'G1', 'G2', 'G3', 'G4', 'G5', 'G6', 'G7', 'G8', 'G9',
             'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'H7', 'H8', 'H9',
             'I1', 'I2', 'I3', 'I4', 'I5', 'I6', 'I7', 'I8', 'I9']

  tuple_practice = (('lebron', 'james'),
                   ('kobe', 'bryant'),
                   ('michael', 'jordan'),
                   ('charles', 'barkley'),
                   ('kareem', 'abdul-jabbar'),
                   ('oscar', 'robertson'),
                   ('hakeem', 'olajuwon'))

orders['coop']['ice_cream'] = ('strawberry', 'vanilla')

The expression:

('pumpernickel',)

is just a tuple of length 1.

  1. Write down the values of the expressions below after the question marks. Make sure you write out the values carefully and legibly and do not leave out curly braces, parentheses, or square brackets, because these tell me you know the type of the object returned. For the example, the answer is the list containing the elements a and b, you must write [‘a’, ‘b’]; writing just ‘a’, ‘b’ will earn a deduction. If an expression does not return a value , you should say None. If an expression will return an error, you should say so. You do not have to say what kind of error, but if you know, you can say so, and if there is an error, but you get the error type wrong, you still get full credit! The first two have been done as an example:

    • orders[‘bogie’]

      >>>  {'tea': ('red', 'red', 'red'),
           'ham_sandwich': ('med', 'well', 'med'),
            'fritata': ('sausage', 'plain', 'bacon')}
      
    • tuple_practice[0]

      >>>   ('lebron','james')
      
    • orders[‘grant’][‘tiramisu’]

      >>>
      
    • orders[‘coop’][‘tea’]

      >>>
      
    • orders[‘grant’][‘tiramisu’][0]

      >>>
      
    • orders[‘grant’][‘tiramisu’][0][1]

      >>>
      
    • orders[‘coop’][‘ice_cream’][1][0]

      >>>
      
    • tuple_practice[3]

      >>>
      
    • tuple_practice[2][1]

      >>>
      
    • squares[2]

      >>>
      
    • squares[11]

      >>>
      
    • squares[-1]

      >>>
      
    • squares[2][0]

      >>>
      
    • squares[4][1]

      >>>
      
    • squares[4][1][2]

      >>>
      
    • squares[0][0:2]

      >>>
      
    • tuple_practice[-1] = (‘errol’,’flynn’)

      >>>
      
    • squares[1][1] = ‘1’

      >>>
      
    • len(tuple_practice[2]) > 2

      >>>
      
  2. Write down expressions to retrieve the given values from the given variables.

    • {'tea': (‘red’, ‘red’, ‘red’), ‘ham_sandwich’: (‘med’, ‘well’, ‘med’), ‘fritata’: (‘sausage’, ‘plain’, ‘bacon’)} from orders:

      >>>
      
    • ''sunny_side_up'' from orders:

      >>>
      
    • 'sausage' from orders:

      >>>
      
    • ( 'red', 'red', 'red' ) from orders:

      >>>
      
    • ('oscar', 'robertson') from tuple_practice:

      >>>
      
    • 'keem' from tuple_practice:

      >>>
      
    • (('michael', 'jordan'),('charles','barkley')) from tuple_practice

      >>>
      
  3. Update orders so that the value associated with the key garbo is the following dictionary:

    {'tea': ('apple_spice', 'earl_grey', 'lipton'),
      'biscuit': ('oat', 'lemon', 'vanilla')}
    

You may assume the following variable assignment has been executed:

teatime = {'tea': ('apple_spice', 'earl_grey', 'lipton'),
           'biscuit': ('oat', 'lemon', 'vanilla')}

>>>
  1. In each of the following examples you are given a piece of code and then asked for the value of a variable after the piece of code is executed. Write down the value of the variable in the space provided. The values for the variables orders, movies, and tuple_practice, used in the first two parts, are still in effect. The first one has been done as an example.

    • Code:

      test_list = [4,5,8,12]
      X = [x * 5 for x in test_list]
      

      Variable:

      >>> X
      >>> [20,25,40,60]
      
    • Code:

      Y = [x[0] for x in tuple_practice]
      

      Variable:

      >>> Y
      >>>
      
    • Code:

      squares[1:3] = ['A1','A2']
      

      Variable:

      >>> squares[2]
      >>>
      
    • Code:

      L = [len(x) for x in tuple_practice]
      

      Variable:

      >>> L
      >>>
      
    • Code:

      F = [len(x) for x in orders['grant']['tea']]
      

      Variable:

      >>> F
      >>>
      
    • Code:

      tables = ['coop','bogie','grant']
      R = [orders[x]['tea'] for x in tables]
      

    Variable:

    >>> R
    >>>
    
    • Code:

      W = [len(x) > 3 for x in movies[0:4]]
      

    Variable:

    >>> W
    >>>
    
    • Code:

      S = [x for x in squares[0:4] if len(x) >= 2]
      

    Variable:

    >>> S
    >>>