Python: why is [False] evaluated as true in an if statement? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

16th Sep 2020, 5:22 PM
Solus
Solus - avatar
3 Answers
+ 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.
17th Sep 2020, 6:07 AM
Ore
Ore - avatar
+ 8
It is a list that is not empty False is not the same as [False]
16th Sep 2020, 5:25 PM
Oma Falk
Oma Falk - avatar
+ 3
In python any non empty object like list, tuple, dictionary or set is treated as True
16th Sep 2020, 5:29 PM
Peter Parker
Peter Parker - avatar