+ 3
Python Dictionary
Below code copied from Sololearn' tutorial, I don't understand why pairs[1] is False? pairs[1] should not be "apple" ? I try this on interpreter to Python 3.7, too; and got the same result, why? pairs = {1: "apple", "orange": [2, 3, 4], True: False, None: "True", } print(pairs.get("orange")) print(pairs.get(7)) print(pairs.get(12345, "not in dictionary"))
5 Antworten
+ 3
here is answer watch this video.
this is advance feature of python.
https://youtu.be/7TL1x3PO8iQ
+ 2
i think its because python read 1 as True.
to prove it try print(1 == True)
and somehow python keep the priority 1 as boolean, instead 1 as int
0
Thanks Taste for explaining!
I think you are right,
thought I still don't understand why to process like this.
I try these below and find:
As key to Dictionary, 0 & 1 is SPECIAL, they are not Integer any more, they are False and True.
>>> pairs = {0:"Zero", 1:"One", 2:"Two", True:"true", False:"false"}
>>> pairs
{0: 'false', 1: 'true', 2: 'Two'}
0
You can update a dictionary entry:
If you first write d[1]="apple"and next d[1]= "strawberry", apple will be overwritten.
(And True in this context == 1.)
0
To HonFu:
Yes, I know this, thank you!