+ 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"))

19th Oct 2018, 7:25 AM
Chen Hunter
Chen Hunter - avatar
5 Respostas
+ 3
here is answer watch this video. this is advance feature of python. https://youtu.be/7TL1x3PO8iQ
19th Oct 2018, 11:39 AM
Maninder $ingh
Maninder $ingh - avatar
+ 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
19th Oct 2018, 7:32 AM
Taste
Taste - avatar
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'}
19th Oct 2018, 9:44 AM
Chen Hunter
Chen Hunter - avatar
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.)
19th Oct 2018, 10:58 AM
HonFu
HonFu - avatar
0
To HonFu: Yes, I know this, thank you!
19th Oct 2018, 11:25 AM
Chen Hunter
Chen Hunter - avatar