Python Forum
difference between «1 in [2] == False» and «(1 in [2]) == False»
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
difference between «1 in [2] == False» and «(1 in [2]) == False»
#1
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.
Reply
#2
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pyinstaller false positives DPaul 2 1,193 Nov-10-2022, 10:46 AM
Last Post: carecavoador
  If function is false search next file mattbatt84 2 1,111 Sep-04-2022, 01:56 PM
Last Post: deanhystad
  Getting false even when data exists sriniyum 12 4,184 May-20-2021, 03:29 AM
Last Post: sriniyum
  how to return False in ternary condition KEYS 7 2,980 Dec-10-2020, 05:32 PM
Last Post: perfringo
  Why is the line crashing program when line is false? ErnestTBass 1 1,755 Aug-23-2020, 07:09 PM
Last Post: deanhystad
  else condition not called when if condition is false Sandz1286 10 5,739 Jun-05-2020, 05:01 PM
Last Post: ebolisa
  While loop = False NectDz 1 1,724 Jun-03-2020, 04:35 PM
Last Post: GOTO10
  While loop doesn't end when False Kanashi 2 2,531 Nov-21-2019, 02:38 AM
Last Post: Kanashi
  Do break operators turn while loop conditions from True to False? Drone4four 5 2,896 Oct-24-2019, 07:11 PM
Last Post: newbieAuggie2019
  Palindrome in Python - result always false RavCOder 13 6,602 Oct-16-2019, 01:38 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020