0
Python: why is [False] evaluated as true in an if statement?
print("A" if [False] else "B") Why is [False] evaluated as true and "A" printed in the above if statement?
3 Antworten
+ 2
It is not a falsey. In python, the falsey values are
1. False
2. 0
3. None
4. Empty string '' or ""
5. Empty collection [], () ...
Anything else is a truthy.
Any truthy will make a condition to pass. Change [False] to False and your condition will fail.
+ 8
It is a list that is not empty
False is not the same as [False]
+ 3
In python any non empty object like list, tuple, dictionary or set is treated as True