Ch. 9: Dictionaries and Sets
After executing, what will the following code print? >>>phonebook = {'Chris':'555−1111', 'Katie':'555−2222', 'Joanne':'555−3333' >>>for key in phonebook: print(key, phonebook[key])
Chris 555-1111 Joanne 555-3333 Katie 555-2222
T/F: Dictionaries are NoT mutable objects.
F
T/F: In a dictionary, keys cannot be of different types.
F
T/F: Keys can be lists or any other type of immutable object?
F
T/F: The following codes are equal: phonebook['sameer'] and phonebook['Sameer']
F
T/F: When using dictionaries, you can use a numeric index to retrieve a value by its position from a dictionary?
F
T/F: Dictionaries are NOT sequences.
T
T/F: String comparisons are case sensitive.
T
T/F: The keys in a dictionary must be immutable objects, but their associated values can be any type of object.
T
T/F: You cannot have duplicate keys in a dictionary.
T
What error code will occur given the following code: >>>phonebook ={'Chris':'555-1111', 'Katie':'555-2222', 'Joanne':'555-3333'} >>>phonebook['Kathryn']
a KeyError
When you want to retrieve a value from a dictionary, what code do you use?
a key
What are the two parts of a dictionary?
a key and a value
Which part of a dictionary element must be immutable?
all of it
What is a dictionary?
an object that stores a collection of data
What function clears the contents of a dictionary
clear()
Using the general format, how can you delete an existing key-value pair from a dictionary?
del dictionary_name[key]
Other than using dictionary_name = {} to create a blank dictionary, how else could you create an empty dictionary?
dictionary_name = dict()
How do you create an empty dictionary?
dictionary_name = {}
What is the general format to retrieve a value from a dictionary?
dictionary_name[key]
Using the general format, how can you add new key-value pairs to a dictionary?
dictionary_name[key] = value
In the general format, what code allows you to iterate over all the keys in a dictionary?
for var in dictionary:
What function gets the value associated with a specified key? If the key is not found, the method does not raise an exception. Instead, it returns a default value.
get(key, default)
How can you prevent a KeyError exception from being raised when you are searching a dictionary to determine whether a key exists within the dictionary? (Write the example code for your answer.)
if 'keyYouWantToFind" in phonebook: print(phonebook['keyYouWantToFind'])
What function returns all the keys in a dictionary and their associated values as a sequence of tuples?
items()
What function returns all the keys in a dictionary as a sequence of tuples?
keys(key, default)
What built-in python function can you use to get the number of elements in a dictionary?
len()
key-value pairs are often referred to as what?
mappings
What function returns the value associated with a specified key and removes that key-value pair from the dictionary? If the key is not found, the method returns a default value.
pop()
What function returns a randomly selected key-value pair as a tuple from the dictionary and removes that key-value pair from the dictionary.
popitem()
Element --- 'start' : 1472 ---- is an element in a dictionary. What is the key? What is the value? (answer key comma space value)
start, 1472
What is a key used for?
to locate a specific value
What function returns all the values in the dictionary as a sequence of tuples.
values()
In a dictionary, when can keys be of different types.
when they are immutable
How do you create a dictionary?
{}