-3

I wrote a code

print(False>True)
print(True>False)

result are

False
True

can someone explain me what is this happening

1
  • 1
    print(int(True)) - print(int(False)) Apr 17, 2021 at 18:17

4 Answers 4

5

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.

2

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).

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

0

Boolean result always return 0 or False for false and 1 or True for true

From : Py Doc

For more clarity :

>>> False
False
>>> False>True
False
>>> 0>1
False
>>> True>False
True
>>> 1>0
True

Not the answer you're looking for? Browse other questions tagged or ask your own question.