What will be the output of the following Python code? [code] >>> import collections >>> a=collections.OrderedDict((str(x),x) for x in range(3))

Minh Phượng

New member
What will be the output of the following Python code?
Code:
>>> import collections
>>> a=collections.OrderedDict((str(x),x) for x in range(3))
>>> a
A. {‘2’:2, ‘0’:0, ‘1’:1}
B. OrderedDict([(‘0’, 0), (‘1’, 1), (‘2’, 2)])
C. An exception is thrown
D. ‘ ‘
 
Answer: b
Explanation: The line of code a=collections.OrderedDict() generates a dictionary satisfying the conditions given within the parenthesis and in an ascending order of the keys.
 

Tìm kiếm

Back
Top