2
>>> 11<8 == 34>67
False

>>> (11<8) == (34>67)
True

Why are the results different? What's going on here?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
The Epic
  • 137
  • 5
  • 4
    It's not intuitive, but `<`, `==`, and `>` are all comparison operators and so the whole expression uses comparison chaining. The first one is equivalent to `11 < 8 and 8 == 34 and 34 > 67`. See the linked questions for details. – John Kugelman Jan 08 '21 at 08:14
  • 3
    It's called comparison operator chaining. All comparisons in a row are equivalently combined with `and`. For example `a < b < c` becomes `(a < b) and (b < c)`. – Klaus D. Jan 08 '21 at 08:15
  • @JohnKugelman I looked each of the duplicates, and while it is *linked* to this question here, none of these questions/answers directly answer this question here in a simple manner. I suggest reopening. Having a good concrete example (like OP gave) is useful. – Basj Jan 08 '21 at 08:24
  • @Basj How about [this one](https://stackoverflow.com/questions/6074018/why-does-the-expression-0-0-0-return-false-in-python)? – John Kugelman Jan 08 '21 at 08:33
  • Yes this one is better @JohnKugelman, thanks for sharing! – Basj Jan 08 '21 at 08:50

0 Answers0