Exercise: Python

Questions for: Dictionaries

How do you iterate over the key-value pairs in a dictionary?
A:
for key in dictionary:
B:
for key, value in dictionary.iter_items():
C:
for key, value in dictionary.items():
D:
for key, value in dictionary.get_pairs():
Answer: C
The items() method is used to iterate over key-value pairs in a dictionary.
What is the result of the expression len(dictionary.keys())?
A:
Returns the number of keys in the dictionary.
B:
Returns the number of values in the dictionary.
C:
Returns the total number of key-value pairs.
D:
Raises a TypeError.
Answer: A
The len() function returns the number of keys in a dictionary.
How do you get a list of all values in a dictionary?
A:
dictionary.all_values()
B:
dictionary.get_values()
C:
dictionary.values()
D:
dictionary.extract_values()
Answer: C
The values() method returns a list of all values in a dictionary.
What is the purpose of the dictionary.copy() method?
A:
Creates a shallow copy of the dictionary.
B:
Creates a deep copy of the dictionary.
C:
Copies only the keys of the dictionary.
D:
Copies only the values of the dictionary.
Answer: A
The copy() method creates a shallow copy of the dictionary.
How do you check if a value is present in the values of a dictionary?
A:
value in dictionary
B:
dictionary.contains(value)
C:
value.exists_in(dictionary)
D:
dictionary.has_value(value)
Answer: A
The in keyword is used to check if a value is present in the values of a dictionary.
Ad Slot (Above Pagination)
Quiz