<分区>
这背后有什么逻辑吗?
>>>'a' and 'b' and 'c'
'c'
>>>'a' or 'b' or 'c'
'a'
标签 python python-2.7
<分区>
这背后有什么逻辑吗?
>>>'a' and 'b' and 'c'
'c'
>>>'a' or 'b' or 'c'
'a'
最佳答案
是的,有逻辑, bool 逻辑。
Python 中的 bool 运算符是惰性的,因此它们返回第一个证明整个语句为真的值。
'a' and 'b' and 'c'
返回 c
因为这是它第一次可以证明整个陈述是正确的。
'a' or 'b' or 'c'
返回 a
因为它是 True,所以语句中的其余值无关紧要。
关于python - 在Python中,为什么 "' a'和 'b'和 'c'“是 'c',而 "' a'或 'b'或 'c'”是 'a'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32229193/