+ 3

Can anyone explain the output of the below mentioned code??

my_dict = {True:'yes',1:'no',1.0:'maybe'} print(my_dict) Output: { True : 'maybe'} I'm not able to understand why the entire dictionary is not printed as the output

6th Nov 2019, 6:40 AM
Shagun Sharma
Shagun Sharma - avatar
7 Respuestas
+ 3
It is actually pretty confusing due to the fact that you are using keywords and mixing integers with decimals. Python does a lot of stuff the way it wants it to. I would prefer a more explicit definition. Try using keys and values that you can easily follow, like names and ages.
6th Nov 2019, 7:31 AM
Fernando Pozzetti
Fernando Pozzetti - avatar
+ 2
I think dictionaries filter their key duplicates by their equality.
6th Nov 2019, 7:01 AM
Seb TheS
Seb TheS - avatar
+ 2
Thank you for your explanation... But i'm still confused😅 Why the keyword True is printed with output 'maybe' and not with its original value 'yes' ?!
6th Nov 2019, 7:25 AM
Shagun Sharma
Shagun Sharma - avatar
+ 2
Actually this question was asked in a challenge... But I agree that booleans should not be used as keys in a dictionary.
6th Nov 2019, 7:38 AM
Shagun Sharma
Shagun Sharma - avatar
+ 1
SC Sharma it could be because you 1.0:'maybe' was put there later and program treated it as reassigning to True:'yes', because 1.0 == True.
6th Nov 2019, 7:31 AM
Seb TheS
Seb TheS - avatar
+ 1
It's a terrible question hahah. You can use online python compilers to try the challenges later
6th Nov 2019, 7:46 AM
Fernando Pozzetti
Fernando Pozzetti - avatar
0
Try this format (released is the dict). released = {     "iphone 3G" : 2008,     "iphone 3GS" : 2009,     "iphone 4" : 2010,     "iphone 4S" : 2011,     "iphone 5" : 2012   } print(released) # Outputs all keys and values print(len(released)) # Outputs 5 print("iphone" in released) # Prints False print("iphone 5" in released) # Prints True
6th Nov 2019, 7:13 AM
Fernando Pozzetti
Fernando Pozzetti - avatar