Truthy and Falsy Values
Learn how Python treats strings, numbers, and booleans as truthy or falsy, and when to use bool() versus exact comparisons.
Sounds useful?
Jump into the interactive version.
No setup. No account. Just Python.
Truthy and Falsy Values
Learn how Python treats strings, numbers, and booleans as truthy or falsy, and when to use bool() versus exact comparisons.
Jump into the interactive version.
No setup. No account. Just Python.
Python does not only work with True and False. It can also treat regular values like strings and numbers as boolean-like values when needed. This Block explains what it means for a value to be truthy or falsy, using simple examples with bool(), strings, numbers, and real boolean values.
You will see why an empty string is falsy, why 0 is falsy, why "False" is still truthy, and why quotation marks matter more than they look like they should. Tiny detail, big consequences.
What you will learn:
bool() to inspect how Python treats a value0 and 0.0 are falsy, while non-zero numbers are truthyname == ""not with truthy and falsy values without making your code mysteriousBy the end, you will understand one of those Python ideas that shows up everywhere later: simple, useful, and just sneaky enough to deserve its own Block.
Ready to practice this in the browser?
> Play now