I'm using python 3.7.4.
I can't figure out why
2 > 3 > -1 == False
while
(2 > 3) > -1 == True
and
2 > (3 > -1) == True ??
I'm using python 3.7.4.
I can't figure out why
2 > 3 > -1 == False
while
(2 > 3) > -1 == True
and
2 > (3 > -1) == True ??
Try 0 == False
and 1 == True
. You will see that boolean are a subclass of int
in Python.
Moreover, operators are read from left to right and the parentheses have higher precedence over the >
operator.
So the last two ones read as follow:
(2 > 3) > -1 -> True > -1 -> 1 > -1 -> True
2 > (3 > -1) -> 2 > True -> 2 > 1 -> True
For the first one you gave, Python reads it as following:
2 > 3 > -1 -> (2 > 3) and (3 > -1) -> False and (3 > -1) -> False
This is related to chaining comparison a op b op c
is equivalent to (a op b) and (b op c)
2 > 3 > -1
--> (2 > 3) and (3 > -1)
which results in False and True
which gives False
(2 > 3) > -1
--> Python will compute the brackets first (2 > 3) = 0 or False
then (0 > -1) = True or 1
2 > (3 > -1)
--> (3 > -1) = True or 1
then (2 > 1) = True or 1