Search code examples
pythonboolean-operations

Python comparison weirdness


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????

Solution

  • 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 150>0 is True.