Explanation:
1. Complete Code
print(dict(zip("ABC", range(3)))["B"])
2. range(3)
First, Python evaluates:
range(3)
This generates:
0, 1, 2
So we have:
"ABC" → A B C
range → 0 1 2
3. zip("ABC", range(3))
zip() pairs the elements from both sequences:
zip("ABC", range(3))
creates pairs conceptually like:
('A', 0)
('B', 1)
('C', 2)
4. dict()
Now dict() converts those pairs into a dictionary:
dict(zip("ABC", range(3)))
The resulting dictionary is:
{'A': 0, 'B': 1, 'C': 2}
5. ["B"] — Dictionary Lookup
Now Python accesses the value associated with key "B":
{'A': 0, 'B': 1, 'C': 2}["B"]
The value of "B" is:
1
6. print()
Finally:
print(1)
displays the result.
✅ Final Output
1

0 Comments:
Post a Comment