1

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?

Xantium
  • 11,201
  • 10
  • 62
  • 89
  • 2
    It's because `bool` is a subclass of `int`. `True` has a value of 1, while `False` has a value of 0. Related questions: [this](https://stackoverflow.com/questions/8169001/why-is-bool-a-subclass-of-int) and [this](https://stackoverflow.com/questions/2764017/is-false-0-and-true-1-in-python-an-implementation-detail-or-is-it-guarante) – Aran-Fey Mar 26 '18 at 12:14
  • the question is slightly different. OP is wondering why results return an `integer` when they could return a `boolean`. `boolean` _could_ be a subclass and still return a boolean type when it fits the range (ok it would be confusing) – Jean-François Fabre Mar 26 '18 at 12:18
  • @Jean-FrançoisFabre That's not how I read the question, but I wouldn't be opposed to the question being reopened. It's not a great dupe anyway. – Aran-Fey Mar 26 '18 at 12:20
  • I agree on "not a great dupe" :) But I don't like using my gold badge powers to reopen against someone else. – Jean-François Fabre Mar 26 '18 at 12:22
  • @Aran-Fey I disagree (and am somewhat unhappy) with this duplicate, but I will trust your judgement. So it's as basic as `1 + 1 = 2` – Xantium Mar 26 '18 at 12:59
  • @Simon If your question is just "Why can I do math with booleans", then I think it's enough to explain that booleans are a subclass of ints (so in that case I'd say the duplicate is appropriate). But if, as Jean-François Fabre said, you're asking "Why does this return an int instead of a boolean?", then the question should be reopened. FWIW, I've cast a reopen vote. – Aran-Fey Mar 26 '18 at 14:58

2 Answers2

1

Basically True is equal to the integer 1 and False is equal to the integer 0. This is because Boolean is a subclass of int.

You can get these values by converting the bools into ints:

int(True)
int(False)
Tim Woocker
  • 1,883
  • 1
  • 16
  • 29
1

boolean is a subclass of int. False and True have resp. 0 and 1 values.

When performing an addition, the addition method of int is used, boolean doesn't redefine either __add__ or __sub__ (or mul or div...) so the result is of int type, even if False + True or False + False could fit in a boolean type (an addition method which would return an integer or a boolean depending on the range would be a bit weird).

>>> bool.__add__ is int.__add__
True
>>> bool.__sub__ is int.__sub__
True

and type(a / a) is float because of python 3 floating point division even between integers. If you need int, do type(a // a)

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219