Computer Science
What will the following code do?
d = {"Phy":94, "Che":70, "Bio":82, "Eng":95}
d.update({"Che":72, "Bio":80})
- It will create new dictionary as dict={"Che":72,"Bio":80} and old d will be deleted.
- It will throw an error as dictionary cannot he updated.
- It will simply update the dictionary as dict={"Phy":94, "Che":72, "Bio":80, "Eng":95}.
- 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
What is printed by the following statements ?
D1 = {"cat":12,"dog":6,"elephant":23,"bear":20} print(25 in D1)- True
- False
- Error
- None
What will be the result of the following code ?
d1 = {"abc":5,"def":6,"ghi":7} print(d1[0])- abc
- 5
- {"abc":5}
- Error
What will be the result of the following code?
d = {"Jo":1,"Ra":2} d.update({"Phoebe":2}) print(dict)- {"Jo":1,"Ra":2,"Ph":2}
- {"Jo":1,"Ra":2}
- {"Jo":1,"Ph":2}
- Error
Which of the following will delete key_value pair for key="tiger" in dictionary?
di = {"lion":"wild","tiger":"wild","cat": "domestic", "dog":"domestic"}- del di["tiger"]
- di["tiger"].delete( )
- delete(di["tiger"])
- del(di.["tiger"])