Understanding Python's "Truthiness"
(Adapted from a post I wrote on Quora...)
In many languages, the expression which follows the if keyword must be a Boolean expression–that is, it must evaluate to True or False. Of course we can write such if statements in Python:
>>> a = 1
>>> if a == 1:
... print('yes')
...
yes
The if statement at line 2 clearly evaluates to True, i.e., the value of the variable a is equal to 1.
Python, like some other programming languages, allows you to put any expression after the if, e.g.,
>>> if a:
... print('truthy')
...
truthy
(The above would not work in my second favorite language, Go, but it works in Python.)
The question is–Why does it work?
Python follows a model of truthiness–that is, any non-zero value is considered True, whereas any zero value is considered False. Let’s see it in action:
>>> if 0:
... print('this should not be printed')
...
>>> if 0.0:
... print('ditto for this')
...
>>> if -3:
... print('-3 is True')
...
-3 is True
If the above is confusing, think of it as if we had written
!= False
after those numbers, i.e.,
>>> if -3 != False:
... print('-3 is True')
...
-3 is True
Of course, we’d never write the above, but it is equivalent.
It turns out the story is a bit more interesting. Any non-empty container is considered True (and conversely, any empty container is considered False).
What’s a container? Well, it’s an object that contains 0 or more things. In this case, the circular definition serves us well:
- A list is a container:
>>> my_list = ['this', 'that', 'other']
>>> if my_list:
... print('list is non-empty')
...
list is non-empty
- A string is a container:
>>> s = 'this is a string'
>>> if s:
... print('string is non-empty')
...
string is non-empty
- A dictionary is a container:
>>> d = { 'one': 1 }
>>> if d:
... print('dict is non-empty')
...
dict is non-empty
…and tuples and sets are also containers.
We can demonstrate the False condition similarly:
>>> if []:
... print("[] is considered False, so this won't be printed")
...
>>>
>>> if '':
... print("'' is considered False, so this won't be printed")
...
>>>
…and so on.
Next time you write an if statement in Python, remember the concept of truthiness!
One final note…sometimes the brevity introduced by truthiness is not such a good idea. Consider the first example above–checking to see integer variable a is not 0.
>>> if a:
... print('…')
...
While it’s correct, one might argue that the above syntax obscures the fact that a could be negative. I’d probably write (and indeed have written in the past):
if a != 0
in a case where I wanted the reader to understand that a could be negative. It’s a matter of taste, to be sure, but I always recommend that beginners err on the side of clarity. Of course, that applies to experts too! Remember the wise words of Harold Abelson: “Programs must be written for people to read, and only incidentally for machines to execute.”
Thanks for the clear explanation of Python’s “truthiness” concept! Your examples really helped solidify the understanding. Emphasizing clarity in coding is key, and your post does an excellent job of conveying that. Great work! 🐍👍