Home > Blockchain >  comparing an equation in python giving False upon comparison
comparing an equation in python giving False upon comparison

Time:12-31

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?

CodePudding user response:

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
  • Related