Surprise to find that False==0 and True == 1, in python ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Surprise to find that False==0 and True == 1, in python ?

just discover that False==0 like in C (and True==1) dict={1:1} dict[False]=42 dict[0]=888 dict[True]=7 print(dict) return {False:888, 1:7} PS: work with array too I thought Boolean would have been a separate case in Python?

6th Feb 2018, 1:02 PM
Mikle Kobe
Mikle Kobe - avatar
1 Answer
+ 9
According to Python docs, Boolean subsection. "Boolean values behave like the values 0 and 1, respectively, in almost all contexts, the exception being that when converted to a string, the strings "False" or "True" are returned, respectively." Any integer except for 0, when converted to bool, is True.
6th Feb 2018, 1:14 PM
Hatsy Rei
Hatsy Rei - avatar