Dictionaries why False? | SoloLearn: Learn to code for FREE!

0

Dictionaries why False?

In the code blow the key 1 exists. Why do I get False instead of "apple"? https://code.sololearn.com/crBCZ05rwLwg

3 Answers

New Answer

+4

True, when it comes to dicts, is equivalent to 1. So when you add True as a key to your dict, 1 is overwritten.

+2

+1

python secretly do, **type coercion** (not surprised since python is an OOP language). def foo():return true print( foo() == 1). # true To stop this coercion of a thing, you have to be strict with your object referencing. print(foo() is 1). # false; print(foo() is true). # true