Search code examples
pythonbooleanlogicboolean-logicboolean-expression

Why is bool([]) == False while [] == False is False and not True in python boolean logic


I've just started learning Python and i was trying this

[] == False #False 

but :

bool([]) #False

from what i got values like [],0 .. are False what did i missed exactly and thanks!


Solution

  • The operator == is very literal. If the 2 things you are comparing are not exactly the same (this includes types, like "2" == 2 is False) then the result will always be False. So the boolean False is not literally the same thing as an empty list [] which is why [] == False is False.

    An empty list is just treated as "False" when converted to a boolean, which you did with bool([]). So the output of bool([]) is False which is literally the same as False. Thus bool([]) == False is True.