我想知道为什么[]和{}在python中返回[]?
>>> [] and {}
[]
编辑:为什么[]和{}为假?我理解和/或表达,但我无法理解[]和{}表达式是假的?
答案 0 :(得分:3)
在仅包含运算符and
(可能是多个)和多个运算符的短路评估中,表达式返回第一个 falsy 值,在这种情况下为[]
- 一个空列表。
如果所有值都是 truthy ,则返回最后一个值。
# [] and {} both are falsey
In [77]: [] and {}
Out[77]: []
# 3 is truthy, {} is falsey
In [78]: 3 and {}
Out[78]: {}
# all except {} are truthy
In [79]: 3 and 9 and {}
Out[79]: {}
# you get it...
In [80]: 3 and 9 and {} and 10
Out[80]: {}