print(False + True)
Why does this return an integer 1
Is there any explanation?
Also print((int(False + True) == (False + True)) )
returns True, so this means that True + False is indeed an integer ?
print(False + True)
Why does this return an integer 1
Is there any explanation?
Also print((int(False + True) == (False + True)) )
returns True, so this means that True + False is indeed an integer ?
From the python 3 docs The standard type hierarchy:
Booleans (bool)
These represent the truth values False and True. The two objects representing the values False and True are the only Boolean objects. The Boolean type is a subtype of the integer type, and 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.
So yes, they are a subtype of integer and behave mostly like them.
(Python 2 operates differently, but it is end-of-life so I am not adding its rules)
bool
is a subclass of int
:
>>> bool.mro()
[<class 'bool'>, <class 'int'>, <class 'object'>]
>>> issubclass(bool, int)
True
print(False + True)
returns 1 because
int(True)
= 1 and int(False)
= 0
True has a value of 1 and False has a value of 0. They are binary values. You can use them interchangeably with their integer equivalents.
Not Integers per say, they are binary digits {True:1, False:0}