0

With these statements:

>>> "foo" in "foobar"
True
>>> "foo" in "foobar" is True
False
>>> ("foo" in "foobar") is True
True

Why is the second statement not returning True?

kontur
  • 4,934
  • 2
  • 36
  • 62
  • 1
    [Comparison chaining](https://docs.python.org/3/reference/expressions.html#comparisons) - `"foo" in "foobar" is True` -> `("foo" in "foobar") and ("foobar" is True)` -> `True and False` -> `False`. – jonrsharpe Jun 17 '21 at 08:33
  • See also https://stackoverflow.com/q/49893766/3001761 – jonrsharpe Jun 17 '21 at 08:35
  • Okay, wow. This is super unintuitive for the `in` comparative. So it helps to think of this as akin to a `0 < 5 < 10` comparison, correct? – kontur Jun 17 '21 at 08:39
  • 1
    Yes, exactly - that chaining happens for _all_ comparison operators. – jonrsharpe Jun 17 '21 at 08:40

0 Answers0