【发布时间】:2015-11-20 15:28:12
【问题描述】:
这背后有什么逻辑吗?
>>>'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'
【问题讨论】:
标签: python python-2.7
是的,有逻辑,布尔逻辑。
Python 中的布尔运算符是惰性的,因此它们返回证明整个语句为 True 的第一个值。
'a' and 'b' and 'c' 返回c,因为这是它第一次证明整个陈述为真。
'a' or 'b' or 'c' 返回 a,因为它是 True,所以语句中的其余值无关紧要。
【讨论】: