-3

I wrote a code

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

result are

False
True

can someone explain me what is this happening

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140

4 Answers4

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.

altermetax
  • 696
  • 7
  • 16
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).

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
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

am2021
  • 117
  • 1
  • 4
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
Neeraj
  • 975
  • 4
  • 10