Computer Science

What is the output of the following ?

d = {0: 'a', 1: 'b', 2: 'c'}
for i in d:
    print(i)

1.

0  
1  
2

2.

a  
b  
c

3.

0  
a  
1  
b  
2  
c  

4.

2  
a  
2  
b  
2  
c  

Python Dictionaries

3 Likes

Answer

0  
1  
2

Reason — The code initializes a dictionary d with keys 0, 1, and 2, each mapped to the values 'a', 'b', and 'c' respectively. In Python, when we use a for loop with a dictionary using the in operator, like for i in d:, it iterates over the keys of the dictionary by default. During each iteration, the loop prints the current key i. Therefore, the output will be the keys of the dictionary d, which are 0, 1, and 2.

Answered By

2 Likes


Related Questions