Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading