变量和数字的奇怪逻辑行为

Strange Logic Behavior with Variable and Number

假设我定义 ab 如下:

a = 1
b = 1

那我测试一下:

a == 1
#True

5>4
#True

a==1 & b==1
#True

5>4 & 4>3
#True

a==1 & 5>4
#False

最后一个是怎么回事?我希望能够测试最后一个不等式并得到 True.

的结果

在Python &中是针对数字的位运算,不是逻辑。请改用 andor

Unlike C, all comparison operations in Python have the same priority, which is lower than that of any arithmetic, shifting or bitwise operation. Also unlike C, expressions like a < b < c have the interpretation that is conventional in mathematics:

这意味着:

a==1 & 5>4 is equal to 
a == ( 1 % 5 ) > 4
a == 1 > 4
True > 4

False