Python Chapter 9
This operator can be used to find the intersection of two sets. a. I b. & c. - d. ^
&
When you open a file for the purpose of retrieving a pickled object from it, what file access mode do you use?
'rb'
When you open a file for the purpose of saving a pickled object to it, what file access mode do you use?
'wb'
A ______ contains a collection of unique values and works like a mathematical set.
set
How do you delete an element from a dictionary?
By using the del statement
How do you determine the number of elements that are stored in a dictionary?
By using the len function
What is the number of the first index in a dictionary?
Dictionaries are not indexed by number.
A list can be a dictionary key. T or F
False
The dictionary method popitem does not raise an exception if it is called on an empty dictionary. T or F
False
The elements in a dictionary are stored in ascending order, by the keys of the key-value pairs.
False
The following statement creates an empty set: myset = ( ) T or F
False
The keys in a dictionary must be mutable objects. T or F
False
The union of two sets is a set that contains only the elements that appear in both sets.
False
You can store duplicate elements in a set. T or F
False
What does the keys method return?
It returns all the 'keys' in a dictionary as a sequence of tuples.
What does the values method return?
It returns all the 'values' in the dictionary as a sequence of tuples.
A ______ is an object that stores a collection of data. Each element in a dictionary has two parts: a key and a value. You use a key to locate a specific value.
dictionary
This set method removes an element but does not raise an exception if the element is not found. a. remove b. discard c. delete d. erase
discard
The issubset() method can be used to determine whether set1 is a subset of set2.
True
The remove method raises an exception if the specified element is not found in the set. T or F
True
The set remove and discard methods behave differently only when a specified item is not found in the set.
True
You would typically use a for loop to iterate over the elements in a set.
True
Are the elements of a set ordered or unordered?
Unordered
How do you create an empty set?
You call the built-in set function.
Sets are created using curly braces { }.
False
Sets are immutable.
False
This operator can be used to find the difference of two sets. a. I b. & c. - d. ^
-
This operator can be used to find the union of two sets. a. I b. & c. - d. ^
I
What is the difference between the remove and discard methods?
If the specified element to delete is not in the set, the remove method raises a KeyError exception, but the discard method does not raise an exception.
What does the get method do if the specified key is not found in the dictionary?
It returns a default value.
What does the items method return?
It returns all a dictionary's keys and their associated values as a sequence of tuples.
An element in a dictionary has two parts. What are they called?
Key and value
What will be the result of the following code? ages = {'Aaron' : 6, 'Kelly' : 3, 'Abigail' : 1 } value = ages['Brianna']
KeyError
Does a set allow you to store duplicate elements?
No
______ a object is the process of converting the object to a stream of bytes that can be saved to a file for later retrieval.
Serializing
Which of the following does not apply to sets?
The elements are in pairs.
Which part of a dictionary element must be immutable?
The key
What module do you import if you want to pickle objects?
The pickle module
What is the difference between the dictionary methods pop and popitem?
The pop method accepts a key as an argument, returns the value that is associated with that key, and removes that key-value pair from the dictionary. The popitem method returns a randomly selected key-value pair, as a tuple, and removes that key-value pair from the dictionary.
What is object serialization?
The process of converting the object to a stream of bytes that can be saved to a file for later retrieval.
A dictionary can include the same value several times but cannot include the same key several times.
True
A tuple can be a dictionary key. T or F
True
Dictionaries are not sequences. T or F
True
If you try to retrieve a value from a dictionary using a nonexistent key, a KeyError exception is raised.
True
Sets store their elements in an unordered fashion. T or F
True
The difference of set1 and set2 is a set that contains only the elements that appear in set1 but do not appear in set2.
True
The following statement creates an empty dictionary: mydct = { } T or F
True
How can you determine whether a key-value pair exists in a dictionary?
You can use the in operator to test for a specific key.
How can you determine whether a specific element exists in a set?
You can use the in operator to test for the element.
How do you determine the number of elements in a set?
You pass the set as an argument to the len function.
This operator can be used to find the symmetric difference of two sets. a. I b. & c. - d. ^
^
You can add one element to a set with this method. a. append b. add c. update d. merge
add
The ____ dictionary method returns the value associated with a specified key. If the key is not found, it returns a default value. a. pop( ) b. key( ) c. value( ) d. get( )
get( )
In order to avoid KeyError exceptions, you can check whether a key is in the dictionary using the ________ operator.
in
You can use the ____ operator to determine whether a key exists in a dictionary. a. & b. in c. ^ d. ?
in
Which method would you use to get all the elements in a dictionary returned as a list of tuples?
items
The ____ method returns all of a dictionary's keys and their associated values as a sequence of tuples. a. keys_values( ) b. values( ) c. items ( ) d. get( )
items ( )
In a dictionary, you use a(n) ________ to locate a specific value.
key
Which would you use to get the number of elements in a dictionary?
len
The ____ function returns the number of elements in a dictionary: a. size( ) b. len( ) c. elements( ) d. count( )
len( )
The following function returns the number of elements in a set: a. size( ) b. len( ) c. elements( ) d. count( )
len( )
What function do you call to pickle an object?
pickle.dump
What function do you call to retrieve and unpickle an object?
pickle.load
In Python, object serialization is called ______
pickling
What is the process used to convert an object to a stream of bytes that can be saved in a file?
pickling
Which method would you use to get the value associated with a specific key and remove that key-value pair from the dictionary?
pop
The ____ method returns the value associated with a specified key and removes that key-value pair from the dictionary . a. pop( ) b. random( ) c. popitem( ) d. rand_pop( )
pop( )
The ____ method returns a randomly selected key-value pair from a dictionary. a. pop( ) b. random( ) c. popitem( ) d. rand pop( )
popitem( )
This set method removes an element and raises an exception if the element is not found. a. remove b. discard c. delete d. erase
remove
You use ____ to delete an element from a dictionary. a. the remove method b. the erase method c. the delete method d. the del statement
the del statement
You can add a group of elements to a set with this method. a. append b. add c. update d. merge
update
What is the correct structure to create a dictionary of months where each month will be accessed by its month number (for example, January is month 1, April is month 4)?
{ 1 : 'January', 2 : 'February', ... 12 : 'December' }
You can use ____ to create an empty dictionary. a. { } b. ( ) c. [ ] d. empty( )
{ }
1. What will be displayed after the following code executes? (Note: the order of the display of entries in a dictionary are not in a specific order.) cities = {'GA' : 'Atlanta', 'NY' : 'Albany', 'CA' : 'San Diego'} if 'CA' in cities: del cities['CA'] cities['CA'] = 'Sacramento' print(cities)
{'CA': 'Sacramento', 'NY': 'Albany', 'GA': 'Atlanta'}
What will be displayed after the following code executes? (Note: the order of the display of entries in a dictionary are not in a specific order.) cities = {'Ga':'Atl','NY':'Alb','CA':'San D'} if 'FL' in cities: del cities['FL'] cities['FL']='Tallah' print(cities)
{'CA': 'San Diego', 'NY': 'Albany', 'GA': 'Atlanta'}