Quiz

state_codes = {'MI':'Michigan', 'CA':'California', 
                'IL':'Illinois'}

state_codes['TX'] = 'Texas'
print(state_codes)

What is the output of the code above?

{'MI':'Michigan', 'CA':'California', 'IL':'Illinois'} Error {'MI': 'Michigan', 'CA': 'California', 'IL': 'Illinois', 'TX': 'Texas'} You can add a new key/value on the fly by just assigning the key and value

What is the output when you execute 'print(state_codes['tx'])'

Texas Error None KeyError as the keys are case sensitive and 'TX' is not the same as 'tx'

What is the output when you execute 'print(list(state_codes))'

['Michigan', 'California', 'Illinois', 'Texas'] ['MI', 'CA', 'IL', 'TX'] Error By default all keys are passed on to get a list of keys

What is the output when you execute 'print(list(state_codes.values()))'

['Michigan', 'California', 'Illinois', 'Texas'] ['MI', 'CA', 'IL', 'TX'] Error values() will get all the values of the dictionary of type dict_values which when passed to the list function will convert it to a list object

You can change the value of a key by simply assigning the new value to the key. For e.g., state_codes['MI'] = 'Great Lakes'

True False Yes, indeed!

Which statement correctly deletes a the key/value of the key 'a' of the dictionary `a = {'a':'ab', 'b':'bc'}`

del a['a'] del a Compilation error as the name of the dictionary and the key cannot be the same del a deletes the entire dictionary where as del a['a'] deletes only the particular key with its value

Which method not only deletes the key/value pair but also returns the value from a dictionary?

del a['a'] pop('a') only pop method returns the removed value that can be assigned to a variable

Exercise

results matching ""

    No results matching ""