Computer Science
Which of the following will give error if d1 is as shown below?
d1 = {"a":1, "b":2, "c":3}
- print(len(d1))
- print(d1.get("b"))
- d1["a"] = 5
- None of these
Python Dictionaries
7 Likes
Answer
None of these
Reason — print(len(d1)) will print the length of d1 i.e. 3print(d1.get("b")) will return the value of key "b" i.e. 2d1["a"] = 5 will update the value of key "a" to 5.
Hence, all the expressions above will execute with no error.
Answered By
1 Like
Related Questions
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"])
Which of the following Python codes will give the same output if
dict = {"diary":1, "book":3, "novel":5}(i) dict.pop("book")
(ii) del dict["book"]
(iii) dict.update({"diary":1,"novel":5})- (i), (ii), (iii)
- (1), (ii)
- (i), (iii)
- (ii), (iii)
What will be the output of following Python code?
d1 = {"a":10,"b":2,"c":3} str1="" for i in d1: str1 = str1 + str(d1[i]) + " " str2 = str1[:-1] print(str2[::-1])- 3, 2
- 3, 2, 10
- 3, 2, 01
- Error