Truthiness and falsiness
Truthy and falsey
Python allows many shortcuts with Boolean expressions. Most everything in Python has a truth value. As noted earlier, we refer to the truth values of anything other than Booleans with the whimsical terms “truthy” and “falsey” (we also use the terms “truthiness” and “falsiness”).
When used in Boolean expressions and conditions for loops or branching (if/elif), truthy values are treated (more-or-less) as True
, and falsey values are treated (more-or-less) as False
.
Truthy things | Falsey things |
---|---|
any non-zero valued int or float , |
0 , 0.0 |
5 , -17 , 3.1415 |
|
any non-empty list, | the empty list, [] |
['foo'] , [0] , ['a', 'b', 'c'] |
|
any non-empty tuple, | the empty tuple, () |
(42.781, -73.901) , ('vladimir') |
|
any non-empty string, | the empty string, "" |
"bluster" , "kimchee" |
This allows us to use conditions such as these:
if x % 2:
# it's odd
print(f"{x} is odd")
if not s:
print("The string, s, is empty!")
If you want to know if something is truthy or falsey in Python, you can pass the value or expression to the Boolean constructor, bool()
.
>>> bool(1)
True
>>> bool(-1)
True
>>> bool(0)
False
>>> bool('xyz')
True
>>> bool('')
False
>>> bool(None)
False
Copyright © 2023–2025 Clayton Cafiero
No generative AI was used in producing this material. This was written the old-fashioned way.