I wrote a code
print(False>True)
print(True>False)
result are
False
True
can someone explain me what is this happening
I wrote a code
print(False>True)
print(True>False)
result are
False
True
can someone explain me what is this happening
In Python, when you use booleans in a greater/lower than comparison they are automatically considered as numbers, so True
becomes 1
and False
becomes 0
. Replace them and the answer becomes obvious:
print(0 > 1)
print(1 > 0)
The first check is False
and the second check is True
.
Boolean values are also integers and have an integer value:
>>> type(False)
<class 'bool'>
>>> bool.mro() # base classes include integer
[<class 'bool'>, <class 'int'>, <class 'object'>]
>>> int(False)
0
>>> int(True)
1
So False(0) is not greater than True(1), and True(1) is greater than False(0).
You're basically printing whether or not 0 (False) is greater than 1 (True), which is False, and then vice versa for the second statement