What is output of code:
print({True:'yes', 1:'no', 1.0:'maybe'})
Please explain it with answer
What is output of code:
print({True:'yes', 1:'no', 1.0:'maybe'})
Please explain it with answer
In Python dictionary, you cannot have duplicate keys. What is happening in your case is that all the keys in your dictionary evaluate to the boolean value True
.
True, 1.0 and 1 all are equal to -> True
So what is happening is that first the value with key True
gets a value 'yes' then its value is overridden by 'no'
and finally by 'maybe'
Hence you have a resulting dictionary with only one key True
with value 'maybe'
So basically this is happening:
True:'yes' == True:'yes'
1:'no' == True:'no'
1.0:'maybe' == True:'maybe'
(because 1, 1.0 and True all evaluated to boolian True
)