Python Challenge — Day 22
Source: https://twitter.com/CodingComputing/status/1685852417579966465?s=20
Level: beginner
Question
What is the output of the following code and why?
d = {1:10, 2:20, 3:30}
print(20 in d)
Answer
False
Explanation
So, the first time, the program will be defined d
as a new dictionary and assign {1:10, 2:20, 3:30}
as elements.
In Python, a dictionary is a collection of key-value pairs. and in this dictionary, the first number 1, 2, 3
called as key
and the second number 10, 20, 30
is the corresponding value
.
In the second line, the syntax instructs the computer to print 20
in d
. Remember, it is mean the 20
is from key
not from the corresponding value
.
Because the key
value in d
not have 20
in the key
so it will show False
as the result.