This question already has answers here:
Is False == 0 and True == 1 an implementation detail or is it guaranteed by the language?
(3 answers)
Closed 6 years ago.
>>> True + 2
3
>>> False + 2
2
I can understand that somehow, True means 1 and False means 0 . So
does it mean, a Boolean and integer operation always gives an integer?
In python bool is a subclass of int, and therefor satisfies the "is-a" relation, meaning a bool is-a int.
To demonstrate:
issubclass(bool, int)
=> True
isinstance(True, int)
=> True
In practice this means that in any operation which works on an int, the int can be substituted with a bool.
Related
This question already has answers here:
Is False == 0 and True == 1 an implementation detail or is it guaranteed by the language?
(3 answers)
Closed 2 years ago.
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}
This question already has answers here:
Is False == 0 and True == 1 an implementation detail or is it guaranteed by the language?
(3 answers)
Closed 2 years ago.
I have this example:
a = [-10,10][True]
b = [-10,10][False]
print(a,b)# "10 -10"
Since bool is a subclass of int that's why True acts like 1 and False like 0
This question already has an answer here:
Why in numpy `nan == nan` is False while nan in [nan] is True?
(1 answer)
Closed 3 years ago.
When I check for equality and identity of Python operands e.g. a = []; b = a I get this:
a == b => True
a is b => True
and I understand it.
so, why I am getting diff result with np.nan?:
a = np.nan; b = a
a == b => False
a is b => True
?
Because NaN is never equal to anything else, and
we use == for performing an equality comparison.
On the other hand the object used to represent NaN is identical to itself, because is is used for doing an identity comparison.
This question already has answers here:
Python's Logical Operator AND [duplicate]
(7 answers)
Closed 4 years ago.
I find (boolean type) and (integer) will become a integer in Python3. What conversion happens in this line of code?
flag=True
print(flag and 2)
Output:2
The value of the and operator (and all other Boolean operators) will always be the last value evaluated. Operator and stops evaluation when it evaluates to False so the operand that's evaluated to False will become the value of the evaluation. Conversely, if it evaluates to True, then the operand that's evaluated to True will become the value of the evaluation.
>>> False and 2
False
>>> True and 3
3
>>> 1 and False and 3
False
>>> 1 and 0 and 3
0
This question already has answers here:
Adding the number 1 to a set has no effect
(5 answers)
Closed 9 years ago.
Condider the following example:
>>> {1, True}
set([True])
>>
>>> {True, 1}
set([1])
Why is the set represented differently, depending on the order of the elements?
This happens because 1 and True are equal to each other:
>>> True == 1
True
>>> 1 == True
True
The set retains one element from each equality class.
bool is subclass of int class
>>> issubclass(bool, int)
True
>>> True+1
2
>>> True == 1
True