+ 4
why this is true?
(True > False) == False is false. True > (False == False) is false. why True > False == False is true?
2 ответов
+ 8
True value is 1, False value is 0
1>0 == false
// false.
+ 2
My understanding for the last line is that when Python lets you combine comparison operators like that (WITHOUT using parentheses to make precedence explicit), it's a kind of syntactic sugar.
So in Python something like:
x > y == z
really means:
x > y and y == z
but the first saves you some typing.
And like Nahuel said, True and False behave like 1 and 0. So:
>>> print(True > False == False)
output: True
is because:
>>> print(True > False and False == False)
output: True
As an aside, the one exception to 1 and 0 for True and False is when you convert them to strings.
>>> str(True)
output: 'True'