我对比较运算符感到困惑。例如,比较运算符给出|的不同值&与或相比 - Python
10 or 20 == 20
# output, expected True
10
10 | 20 == 20
(10 | 20) == 20
(10 or 20) == 20
所有3行代码给'假',但我期待'真'。
10 or 20 == 20
# output gives 10, but was expecting True
10
又如:
10 and 20 > 2
# output is as expected
True
(10 and 20) > 2
True
(10 & 20) > 2
# output gives False, but was expecting True
False
最后,如果我这样做:
10 or 20 > 100
#output is 10. No idea why
10
3 or 8 < 200
3
谁能帮助清理这种混乱?非常感谢花时间阅读我的困惑!我正在使用Python 3.6
只需单独打印每个表达式,然后您就会理解其复合的逻辑。例如'(10或20)'是10,但是“(10 | 20)”是30. – Zefick
'10&20'是按位和',所以'01010&10100'(二进制),确实是'00000'这不高于2 – Rafalon