1

I'm not actually able to understand why var or var2 in test is always True.

My code looks like:

if 'test' or 'test1' in somevar:
    print('hue')

It always return TRUE Thanks

  • Python reads it like this: `('test') or ('test1' in somevar)`. Since `'test'` is true, it will always be true. – Loocid Nov 12 '18 at 00:50
  • Have you looked at what `'test' or 'test1' in somevar` actually evaluates to? `print('test' or 'test1' in somevar)` See [Is there a Python equivalent of the C# null-coalescing operator?](https://stackoverflow.com/questions/4978738/is-there-a-python-equivalent-of-the-c-sharp-null-coalescing-operator) for an explanation. – Patrick Haugh Nov 12 '18 at 00:50
  • @Loocid why it does that? – attacker nine Nov 12 '18 at 00:55
  • @attackernine https://docs.python.org/3/reference/expressions.html#operator-precedence `or` is ranked lower than `in` so `in` expressions are evaluated first. – Loocid Nov 12 '18 at 01:19

0 Answers0