Python Programming - Dictionaries

Exercise : Dictionaries - General Questions
  • Dictionaries - General Questions
41.
What is the purpose of the dictionary.update(key=value) method?
Adds a new key-value pair to the dictionary.
Updates the values of the dictionary with new values.
Removes a key-value pair from the dictionary.
Raises a SyntaxError.
Answer: Option
Explanation:
The update() method with key=value adds a new key-value pair to the dictionary.

42.
How do you remove a specific key-value pair from a dictionary?
dictionary.remove(key)
dictionary.delete(key)
dictionary.clear(key)
del dictionary[key]
Answer: Option
Explanation:
The del keyword is used to remove a specific key-value pair from a dictionary.

43.
What is the difference between the dictionary.keys() and dictionary.values() methods?
Both methods return the keys of the dictionary.
Both methods return the values of the dictionary.
dictionary.keys() returns a list of keys, while dictionary.values() returns a list of values.
dictionary.keys() returns a list of values, while dictionary.values() returns a list of keys.
Answer: Option
Explanation:
The keys() method returns a list of keys, and the values() method returns a list of values in a dictionary.

44.
How do you check if a key exists in a dictionary using the get() method?
if key.get(dictionary):
if key.exists_in(dictionary.get()):
if dictionary.get(key) is not None:
if key in dictionary.get():
Answer: Option
Explanation:
The get() method returns None if the key is not present, so checking for is not None verifies key existence.