I have been trying out the use of +
, -
, *
, /
operators on booleans and I do not understand the strange output results I am getting:
First example using the +
:
>>> a = True
>>> b = False
>>> a + b
1
>>> a + a
2
>>> b + b
0
It is clear I am not getting boolean outputs and sure enough:
>>> type(a + a)
<class 'int'>
>>>
Second example using -
:
>>> a - b
1
>>> a - a
0
>>>b - b
0
All integers once again.
Third example using *
:
>>> a * a
1
>>> a * b
0
>>> b * b
0
>>>
And finally using /
(which returned an error):
>>> a / a
1.0
>>> a / b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
>>> b / b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
>>>
Interestingly:
>>> type(a / a)
<class 'float'>
>>>
Returns a float.
What is going on. Why am I getting these integer and float output when using non-boolean operatons and not some Boolean operator as I would expect?