I am wondering about why [] and {} return [] in python?
>>> [] and {}
[]
Edit: Why are [] and {} false?I understand and or expressions , but i cannot understand [] and {} expressions are false?
I am wondering about why [] and {} return [] in python?
>>> [] and {}
[]
Edit: Why are [] and {} false?I understand and or expressions , but i cannot understand [] and {} expressions are false?
In the short circuit evaluation containing only operator and
(could be multiple) and multiple operators, the expression returns the first falsy value, in this case which is []
-- an empty list.
In the case where all the values are truthy, it returns the last value.
# [] 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]: {}