why python give this output ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 3

why python give this output ?

print({True:'yes', 1 : 'no', 1.0 : 'maybe'}) *********OUTPUT**************** {True : 'maybe'}

22nd Jun 2021, 4:15 AM
Jitendra
Jitendra - avatar
12 Answers
- 2
i added : dict have unique keys. that is : {1: "hellO" , 1 : "world" } -----converted into ------>>> {1:"world"} ________________________________________________________________ And True is == 1 therefore {1: "hello" , True: "world"} --------converted into -------> {1 : 'hello', 1 : "world"} (last one) ---------converted into-------> {1 : 'world'}
22nd Jun 2021, 4:41 AM
Jitendra
Jitendra - avatar
+ 5
because 1 and 1.0 are evaluated as True, so result is a dict with only one key with last value^^
22nd Jun 2021, 4:24 AM
visph
visph - avatar
+ 4
Angela all three values are set to same dict key (True), in order of apparence, so only last remain ;)
22nd Jun 2021, 4:37 AM
visph
visph - avatar
+ 4
JITEN.Py (active learner:) { 1: "hello", True: "world" } result to { 1: "world" }, not to { True: "world" } first key, last value, in case of key not string that are evaluated as same values ^^ however, in both case you can access key by any key evalued to same value (that's 1, 1.0 or True)
22nd Jun 2021, 5:03 AM
visph
visph - avatar
+ 1
I’m not 😪
22nd Jun 2021, 4:35 AM
Angela
Angela - avatar
+ 1
now I got it. It takes always the last value, if there are several values for the same key
22nd Jun 2021, 4:52 AM
Angela
Angela - avatar
+ 1
JITEN.Py (active learner:) that's shame to mark its own answer as 'best', all the more after receiving correct explanation and by giving a not correct answer ^^
22nd Jun 2021, 5:07 AM
visph
visph - avatar
+ 1
JITEN Just as future reference, please next time avoid writing code into the tags https://code.sololearn.com/W3uiji9X28C1/?ref=app
22nd Jun 2021, 8:49 AM
Ipang
0
Thanks for clarification
22nd Jun 2021, 5:28 AM
Angela
Angela - avatar
0
Yes
23rd Jun 2021, 1:28 PM
Md muntasir Mamun
- 1
Got it :)
22nd Jun 2021, 4:26 AM
Jitendra
Jitendra - avatar
- 1
visph thx for correction
22nd Jun 2021, 5:05 AM
Jitendra
Jitendra - avatar