Why does calling "1" bring up "False"?

I put in all the original code: 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")) Then I added: print(pairs.get(1)) the output is False for the last print command, can anyone tell me why that is? I thought it was supposed to bring up apple.

1/12/2017 4:11:04 AM

Tibi Gurau

2 Answers

New Answer


Sometimes the best way to fully understand things is to play with the code - run it, have an idea, alter it, run it again and so on until you understand. With this it will help to know that in python true is the same as 1 and false is the same as 0 (it makes sense when thinking of binary rather than decimal). With this in mind play with the code a little, like this for example (and go further until it makes full sense): Take the line True: False, out, run code again, see what happens. - 1 will be apple yes? now put a line in that says True: "weird", and again run and see what happens now add another line after the true: "weird" line that says True: "last entry to true" (so you have two entries to True)


1 is true by default so calling 1 again brings a false