Home > Mobile >  Python comparison weirdness [duplicate]
Python comparison weirdness [duplicate]

Time:09-26

I have an object that I need to compare some attributes of and I can't explain what is going on, but the following is the output of my VS code debugger (I added '=' before each output)

(0 > 55000 | 150 > 280)
=False # expected
250>150
=True # expected
True & False 
=False # expected
(250>150 & (0 > 55000 | 150 > 280))
=True # what????

CodePudding user response:

For numbers, & is "bitwise and" and | is "bitwise or". These operators have higher precedence than comparison operators like > and <.

150 & (0 > 55000 | 150 > 280) == 150 & 0 == 0 then 250>0 is True.

CodePudding user response:

You're using binary bitwise operators & and |. You should be using boolean operators and and or.

CodePudding user response:

| and & are bit operators. bit operators have higher priority better than comparison operator. you should change '|' to "or"and '&' to "and"

  • Related