Question | Click to View Answer |
What does the following code print? player = {"firstName": "Messi", "sport": "soccer"}
print(player["sport"])
|
The |
What does the following code print? city = {
"country": "Brazil",
"hasBeaches": True
}
print(city["hasBeaches"])
|
The The |
What does the following code print? meh = {
"a": 1,
"d": 4,
"e": 99,
}
print(meh["e"])
|
The Notice that the final element in the dictionary is also trailed by a comma. Most programming languages don't allow for this syntax. The "traditional" syntax also works. meh = {
"a": 1,
"d": 4,
"e": 99
}
The Pythonic convention of trailing the final element in a dictionary with a comma is better because it makes it easier to add additional key / value pairs to the dictionary in the future. |
What does the following code print? game = {
"homeScore": 21,
"awayScore": 44,
}
print(game["homeTeam"])
|
This code raises a The |
What does the following code print? fan = {
"noiseLevel": "loud",
"cost": 250,
}
print(fan.keys())
|
The |
What does the following code print? fan = {
"noiseLevel": "loud",
"cost": 250,
}
print(len(fan))
|
The |
What does the following code print? building = {
"numFloors": 3,
"maxCapacity": 50,
}
del(building["maxCapacity"])
print(building)
|
The |
What does the following code print? neighborhood = {
"bestRestaurant": "carmen",
"bestRestaurant": "bruno",
}
print(neighborhood)
|
A dictionary cannot have duplicate keys. When keys are duplicated, Python simply ignores all the duplicate key / value pairs except the last one. |
What does the following code print? cat = {}
cat["firstName"] = "garfield"
print(cat)
|
The |
What does the following code print? sticker = {
"color": "red",
"picture": "ruby",
}
print("color" in sticker)
|
The
|
What does the following code print? bag = dict(
numPockets = 4,
brand = "northface",
)
print(bag)
|
The |
What does the following code print? dict1 = {"mouth": "big"}
dict2 = {"hair": "tall"}
dict1.update(dict2)
print(dict1)
|
The |