KnowledgeBoat Logo
|

Computer Science

What will the following code do?

d = {"Phy":94, "Che":70, "Bio":82, "Eng":95} 
d.update({"Che":72, "Bio":80})
  1. It will create new dictionary as dict={"Che":72,"Bio":80} and old d will be deleted.
  2. It will throw an error as dictionary cannot he updated.
  3. It will simply update the dictionary as dict={"Phy":94, "Che":72, "Bio":80, "Eng":95}.
  4. It will not throw any error but it will not do any changes in dict.

Python Dictionaries

3 Likes

Answer

It will simply update the dictionary as:
d = {"Phy":94, "Che":72, "Bio":80, "Eng":95}.

Reason — The update() method updates the dictionary with the elements from another dictionary object or from an iterable of key/value pairs.
Here {"Che":72, "Bio":80} represents another dictionary with the help of which original d is updated i.e. the value of keys: "Che" and "Bio" are updated to 72 and 80 respectively.

Answered By

1 Like


Related Questions