Computer Science
Which of the following will result in an error for a given valid dictionary D?
- D + 3
- D * 3
- D + {3 : "3"}
- D.update( {3 : "3"})
- D.update { {"3" : 3}}
- D.update("3" : 3)
Answer
- D + 3 — This will result in an error as dictionary does not support + operation.
- D * 3 — This will result in an error as dictionary does not support * operation.
- D + {3 : "3"} — This will result in an error as dictionary does not support + operation..
- D.update( {3 : "3"}) — This will execute with no error since valid dictionary is passed to update( ) method.
- D.update { {"3" : 3}} — This will result in an error since update( ) method syntax used here is invalid.
- D.update("3" : 3) — This will result in an error since update( ) method always takes dictionary as it arguments which is not passed here. Hence it will lead to an error.
Related Questions
What is the use of copy( ) function ?
Discuss the working of copy( ) if
(i) the values are of immutable types,
(ii) the values are of mutable types.
The following code is giving some error. Find out the error and correct it.
d1 = {"a" : 1, 1 : "a", [1, "a"] : "two"}The following code has two dictionaries with tuples as keys. While one of these dictionaries being successfully created, the other is giving some error. Find out which dictionary will be created successfully and which one will give error and correct it :
dict1 = { (1, 2) : [1, 2], (3, 4) : [3, 4]} dict2 = { ([1], [2]) : [1,2], ([3], [4]) : [3, 4]}