NEW
Font size
WorksheetsReview Python Dictionaries
Total questions: 18
Worksheet time: 10mins
You have the following dictionary definition:
d = {'foo': 100, 'bar': 200, 'baz': 300}
What method call will delete the entry whose value is 200 and return 200?
delete d('bar')
d.pop("bar")
d.remove("bar")
None of the above
What would be the output of the following code snippet?
a_dict = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}
for key in a_dict:
print(key)
'blue','apple','dog'
color
fruit
pet
blue
apple
dog
'color':'blue'
'fruit':'apple'
'pet':'dog'
Consider the following statement:
If you only need to work with the values of a dictionary, then you can use .values(), which is a method that returns a list of the dictionary’s values.
Is this statement True or False?
False
True
Items are accessed by their position in a dictionary
True
False
_________________ method will not only delete the item from dictionary, but also return the deleted value.
del ( )
pop( )
Clear( )
erase( )
________________method will returns number of key-value pairs in the given dictionary.
len( )
pop( )
del( )
keys( )
values( )
Which of the following will give an error?
Suppose dict1={"a":1,"b":2,"c":3}
print(len(dict1))
print(dict1.get("b"))
dict1["a"]=5
None of these.
What is the line of code to initialize an empty dictionary called thesaurus?
thesaurus = new Dictionary()
thesaurus = {}
thesaurus = empty_dict()
thesaurus = []
What will the following code output?
"soda"
KeyError
["veggie burger", "salad", "soda"]
["hamburger", "fries", "soda"]
Which code segment when inserted in the blank sets the "Year" from 2006 to 2008?
birthday = {
"Month": "January",
"Day": "01",
"Year":2006
}
__________________________________
OUTPUT: 2008
birthday["Year"]=2008
birthday[Year]=2008
birthday [2008]
birthday[2006]=2008
What will this code do?
birthday = {
"Month": "January",
"Day": "01",
"Year":2006
}
for x, y in birthday.items():
print(x, y)
Prints all the key names in the dictionary 2 at a time
print all the key and value pairs in the dictionary
Prints all the values in the dictionary 2 at a time
Given the dictionary swimmers = {'Manuel':4, 'Lochte':12, 'Adrian':7, 'Ledecky':5, 'Dirado':4}, which of the following lines of code will add an additional key-value pair to the dictionary with "Phelps" as the key and the integer 23 as the value?
swimmers["Phelps"] = 23
swimmers.update({"Phelps":23})
swimmers.setdefault("Phelps",23)
all of the these choices
Given the dictionary d = {'spring': 'autumn', 'autumn': 'fall', 'fall': 'spring'}, what would the following code print?
print (d.get('autumn'))
spring
fall
autumn
none
Given the dictionary d = {'spring': 'autumn', 'autumn': 'fall', 'fall': 'spring'}, what would the following code print?
print (d.get('winter', "spring"))
spring
autumn
none
fall
Given the dictionary d = { 'work': 'success', 'success': 'failure', 'failure': 'money', 'time': 'work', 'industry': 'time'}, in order for this print line to print the word "success", what would need to be the value of x?
print (d[d[x]])
work
industry
time
failure
A dictionary is ordered alphabetically by its keys
true
false
If a dictionary is passed as a parameter to a function, its contents cannot really be changed by the function unless it is returned.
true
false
Consider the dictionary swimmers = {'Manuel':4, 'Lochte':12, 'Adrian':7, 'Ledecky':5, 'Dirado':4}
what is printed with this code:
print(swimmers.get("Adrian") + swimmers.get("Ledecky") + swimmers.get("Phelps", 0))
7 5
"Adrian":"Ledecky"
error
12
