2 回答

TA貢獻54條經(jīng)驗 獲得超303個贊
說實話,這個問題我之前也不知道是為什么,于是抱著求知的心態(tài),問了國外的大神,Михаил Никонов的回答原文如下:
It's because of comparison operator chaining, I think. In Python, expression (x < y < z) is, unlike in C, equivalent to (x < y and y < z), and that can be used for any sequence of comparisons (including membership tests). So basically, without parentheses, your last expression is equivalent to ('a' in 'abc' and 'abc' == True), or so it seems.?
翻譯過來差不多就是,print('a' in 'abc' == True) 這種表達式,在Python里面不像C語言那樣,它必須要增加一個括號:print(('a' in 'abc') == True) 這樣才對,如果不增加括號,就相當于print('a' in 'abc' and 'abc' == True) 結果當然是 False。
添加回答
舉報