What will be the output of the following Python code? [code] >>> import collections >>> a=dict() >>> a=collections.defaultdict(int) >>>

Linh Chi

New member
What will be the output of the following Python code?
Code:
>>> import collections
>>> a=dict()
>>> a=collections.defaultdict(int)
>>> a[1]
A. 1
B. 0
C. An exception is thrown
D. ‘ ‘
 
Answer: b
Explanation: The statement a=collections.defaultdict(int) gives the default value of 0
(since int data type is given within the parenthesis) even if the keys don’t exist in the dictionary.
 

Tìm kiếm

Back
Top