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
The process of arranging the array elements in a specified order is termed as:
- Indexing
- Slicing
- Sorting
- Traversing
Which of the following Python functions is used to iterate over a sequence of numbers by specifying a numeric end value within its parameters ?
- range()
- len()
- substring()
- random()
What is the output of the following ?
x = 123 for i in x: print(i)
- 1 2 3
- 123
- Error
- Infinite loop
Which arithmetic operator(s) cannot be used with strings ?
- +
- *
- -
- All of these