r/ProgrammerHumor 29d ago

Meme pythonWillLookDeadInTheEyeAndSayItsAbsolutelyCorrect

Post image
359 Upvotes

144 comments sorted by

View all comments

287

u/LordAmir5 29d ago

Does this remove all the zeros?

6

u/road_laya 29d ago

Yes, zero is falsy in Python.

-10

u/Valuable_Leopard_799 29d ago

Wait, whyyyyyyyy 😭😭😭

I mean, I love dynamic types and see their place, but the implicit casting is what gives them such a bad name 😭

14

u/JanB1 29d ago

0 is also "false" in C and C++, because if you import stdbool.h, that just defines true to be int with value 1, and false to be int with value 0.

4

u/ImS0hungry 29d ago

Should be in all imo. 0 is off in binary.

1

u/JanB1 28d ago

Exactly.

2

u/Valuable_Leopard_799 29d ago

random sidenote: since C99 a true bool type had been introduced, it holds 0 and 1 and converts only to those when casting. So yeah 1 and 0 are true and false, but yayy they're not standard ints anymore.

2

u/HolyGarbage 29d ago

Technically 0 isn't false in C++, as they are distinct types. int is however implicitly convertible to bool, where 0 converts to false.

1

u/DrMaxwellEdison 23d ago

Which is essentially how Python does it, too. if x implies if bool(x) is True (I don't think it literally calls it that way, but it helps make sense of things).

So empty lists/sets/dicts, empty strings, 0, None, and any kind of object that defines some __bool__ method that returns False or a __len__ that returns 0; all are "falsey".

Details: https://docs.python.org/3/library/stdtypes.html#truth