+ 4
Can you explain this?
I happened to print(True + False) and waited for an error but surprisingly I got a result of 1. Interesting 🤔
9 Réponses
+ 9
+ is not an operator for logical (boolean) expressions, so in this case, to cope with the situation the boolean expressions are treated as numbers, as others had explained.
+ 7
True=1
and
false=0
+ 5
It seems that Python implicitly converts boolean values to integer while using regular mathematical operators.
Moreover, we can see that 'bool' and 'int' types define the exact same methods, like if 'bool' was a subset of 'int'. And it is actually the case!
issubclass(bool, int) # True
That means that booleans in Python are nothing more than integers :
- 0 => False
- any other value => True
+ 3
Thanks now I think I get it, so y'all are saying that True and False are given the values 1 and 0 by default in python and these booleans are integers
+ 3
Correct
+ 2
Granger is mask a built in keyword or I can use any word,
Thanks for the deep explanation though 😉