1

I am comparing an equation in python:

-1 < 2 == 1

it gives False as output

The Left Hand side of the ==:

-1 < 2 which evaluates to True

Right Hand side of the == is:

1

On comparing L.H.S==R.H.S

True==1

which should evaluate to True?

Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44
  • 1
    It works when using brackets `(-1 < 2) == 1` so I think the `==` normally has priority over `<` for whatever reason – rauberdaniel Dec 27 '22 at 17:45
  • I should say that `-1 < 2 == 1` *(even if add parentheses to make it work as expected)* is something that is unlikely to be used in real code, so the problem is somewhat artificial. – Olvin Roght Dec 27 '22 at 17:50

1 Answers1

4

From the docs: Comparisons

Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false).

So the equation becomes:

-1 < 2  and 2 == 1
True and False
False
Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44