Python Forum
difference between «1 in [2] == False» and «(1 in [2]) == False» - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: difference between «1 in [2] == False» and «(1 in [2]) == False» (/thread-25752.html)



difference between «1 in [2] == False» and «(1 in [2]) == False» - fbaldit - Apr-10-2020

Hi,
could someone here explain to me why the first boolean expression of thread subject (namely « 1 in [2] == False ») evaluates to False whereas second (namely «(1 in [2]) == False ») gives True? (checked under python3 interpreter)

Thank's in advance,
F.B.

P.S.1: I know well this could/should better be rewritten «1 not in [2]»...but it doesn't explain why both expressions don't evaluate with the same result.

P.S.2: I first thought that this strange behaviour had to do with operator precedence, but I found on the web that « in » and « == » operators have same priority, so the question remains.


RE: difference between «1 in [2] == False» and «(1 in [2]) == False» - buran - Apr-10-2020

Operator precedence - in and == have same predence
comparison operators can be chained arbitrarily - in the first case 1 in [2] == False is same as 1 in [2] and [2] == False which result in False
in the second example (1 in [2]) == False (1 in [2]) is evaluated first and at the end it is same as False == False which is True.


RE: difference between «1 in [2] == False» and «(1 in [2]) == False» - fbaldit - Apr-20-2020

Thank's a lot for your response!
Everything is now clear to me.

Nevertheless, I must say that here Python syntax reachs the point where balance between compacity of notations and clarity can be discussed...It's not so natural (at least to me) to think that «in» and «==» operator can be chained in ANY case (which is allowed by Python): to me, to give proper meaning to «x in y == z» y should be a sequence, as should also z.

But that's another subject.

Thank's again for your help.