Python - HW9
This operator can be used to find the intersection of two sets.
&
This operator can be used to find the difference of two sets.
-
Match the correct output to the code snippet shown dct = {1:[0,1], 2:[2, 3], 3:[4, 5]}for k in dct:print(k) dct = {'Monday':1, 'Tuesday':2, 'Wednesday':3}print(dct['Tuesday']) dct = {'Monday':1, 'Tuesday':2, 'Wednesday':3}print(dct.get('Monday', 'Not found')) dct = {'Monday':1, 'Tuesday':2, 'Wednesday':3}print(dct.get('Friday', 'Not found')) dct = {1:[0, 1], 2:[2, 3], 3:[4, 5]}print(dct[2]) dct = {'aaa' : 1, 'bbb' : 12, 'ccc' : 123}print(dct['bbb'])
1 2 3 2 1 Not found [2, 3] 12
Assume the variable dct references a dictionary. Write an if statement that determines whether the key 'Jim' exists in the dictionary. If so, delete 'Jim' and its associated value.
ANS: if 'Jim' in dct: del dct['Jim'] else print('Jim is not in the dictionary.')
A list can be a dictionary key.
False
The dictionary method popitem does not raise an exception if it is called on an empty dictionary.
False
The following statement creates an empty set: my_set = {}
False
The keys in a dictionary must be mutable objects.
False
You can store duplicate elements in a set.
False
A tuple can be a dictionary key.
True
Dictionaries are not sequences.
True
Sets store their elements in an unordered fashion.
True
The following statement creates an empty dictionary: my_dict = {}
True
The remove method raises an exception if the specified element is not found in the set.
True
This operator can be used to find the symmetric difference of two sets.
^
You can add one element to a set with this method.
add
This set method removes an element, but does not raise an exception if the element is not found.
discard
The _________ dictionary method returns the value associated with a specified key. If the key is not found, it returns a default value.
get()
Assume the variable dct references a dictionary. Write an if statement that determines whether the key 'James' exists in the dictionary. If so, display the value that is associated with that key. If the key is not in the dictionary, display a message indicating so.
if 'James' in dct: print(dct['James']) else: print('James is not in the dictionary.')
You can use the ____________ operator to determine whether a key exists in a dictionary.
in
The _________ method returns all of a dictionary's keys and their associated values as a sequence of tuples.
items()
The ___________ function returns the number of elements in a dictionary:
len()
The following function returns the number of elements in a set:
len()
Write a statement that creates a dictionary called my_dict containing the following key-value pairs: 'a' : 1 'b' : 2 'c' : 3
mydict = {'a':1, 'b':2, 'c':3}
The _________ method returns the value associated with a specified key and removes that key-value pair from the dictionary.
pop()
This set method removes an element and raises an exception if the element is not found.
remove
You use _____________ to delete an element from a dictionary.
the del statement
You can add a group of elements to a set with this method.
update
Match the correct output to the code snippet shown myset = set('abcdef') myset myset = set(4) myset myset = set('a bb ccc dddd') myset myset = set(['a', 'b', 'b', 'b', 'c', 'c', 'c']) myset myset = set(['a', 'bb', 'ccc', 'dddd']) myset myset = set('a b c') print(len(myset))
{'a', 'b', 'c', 'd', 'e', 'f'} Type Error {' ', 'a', 'b', 'c', 'd'} {'a', 'b', 'c'} {'a', 'bb', 'ccc', 'dddd'} 4
Match the correct output to the code snippet shown set1 = set(['a', 'b', 'c', 'd']) set2 = set(['d','e','f']) set3 = set1.union(set2) set3 set1 = set(['a', 'b', 'c', 'd']) set2 = set(['b', 'c', 'e', 'f']) set3 = set1.intersection(set2) set3 set1 = set(['d', 'e', 'f']) set2 = set(['a', 'b', 'c', 'd', 'e']) set3 = set1.difference(set2) set3 set1 = set(['d', 'e', 'f']) set2 = set(['a', 'b', 'c', 'd', 'e']) set3 = set2.difference(set1) set3 set1 = set(['d', 'e', 'f']) set2 = set(['a', 'b', 'c', 'd', 'e']) set3 = set1.symmetric_difference(set2) set3 set1 = set(['a', 'b', 'c', 'd', 'e']) set2 = set(['a', 'b', 'c']) set3 = set2.issubset(set1) set3
{'a', 'b', 'c', 'd', 'e', 'f'} {'b', 'c'} {'f'} {'a', 'b', 'c'} {'a', 'b', 'c', 'f'} True
You can use __________ to create an empty dictionary.
{}
This operator can be used to find the union of two sets.
|