+ 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 🤔

18th Jul 2020, 10:25 AM
Aphiwe Ayanda Khumalo
6 Respostas
+ 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.
18th Jul 2020, 10:50 AM
Ipang
+ 7
True=1 and false=0
18th Jul 2020, 10:30 AM
Alexander Thiem
Alexander Thiem - avatar
+ 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
18th Jul 2020, 10:52 AM
Théophile
Théophile - avatar
+ 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
18th Jul 2020, 11:58 AM
Aphiwe Ayanda Khumalo
+ 3
Correct
18th Jul 2020, 11:59 AM
Alexander Thiem
Alexander Thiem - avatar
+ 2
Granger is mask a built in keyword or I can use any word, Thanks for the deep explanation though 😉
18th Jul 2020, 12:10 PM
Aphiwe Ayanda Khumalo