Dictionary Operations

The questions below are due on Thursday June 24, 2021; 11:00:00 AM.
 
You are not logged in.

Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.

Below is a Python program, split up into several pieces. After each print statement, provide the value that will be printed to the screen.

If evaluating the print statement in question would cause an error, write error in the box.

a = {'cat': [7, 8, 9], 'coca': 'cola', 'thing': 42}
print(len(a))

Relevant Reading - len

print(list(a.keys()))

Relevant Reading - .keys()

print(a[thing])

print(a['thing'])

Relevant Reading - looking up values

print(a['cat'][-1])

print('cola' in a)

Relevant Reading - in operator

print('coca' in a)

print('ola' in a['coca'])

print(a.get('coca', 20))

Relevant Reading - get operator

print(a.get('something', 27))

b = {'name': 'MIT',
     'students': [{'name': 'John Doe', 'id': 910284619}, 
                  {'name': 'Rafael Reif', 'id': 999999999}],
     'location': {'state': 'MA', 'country': 'USA'}}
     
print(b['location']['state'][0])

Next Exercise: Dictionary Swap

Back to exercises