我對比較運算符感到困惑。例如,比較運算符給出|的不同值&與或相比 - 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