Let's talk about truthiness in Python.
We have a list of numbers:
>>> numbers = [2, 1, 3, 4, 7]
and we have an
if statement that either prints
no based on the condition "
>>> if numbers: ... print("yes") ... else: ... print("no") ... yes
Note that this condition isn't checking
len(numbers) == something or some other expression involving
The condition is just "
numbers"; that's it!
yes when we evaluated that
if statement, despite the fact that
numbers is not
>>> numbers == True False
numbers variable points to a list, but
True is a boolean.
But if we convert
numbers to a boolean we will get
>>> bool(numbers) True
Python is checking the truthiness of
Truthiness is about asking the question: if we converted this object to a boolean, what would we get?
if statements are all about truthiness-checking.
Whenever you execute an
if statement, Python will check the result of the condition and implicitly convert it to a boolean (if it's not already) to check the truthiness of that object.
In fact, all boolean expressions check for truthiness.
If we use the
not operator on
>>> not numbers
This will implicitly convert
numbers to a boolean and then negate whatever we get back (as if we said
>>> not numbers False
not numbers is really checking the falsiness of
numbers (where falsiness is the opposite of truthiness).
Truthiness and falsiness are not mentioned anywhere in the Python documentation. Python calls this truth value testing. But Python programmers in the wild rarely talk about "truth value testing". Colloquially we always say truthiness and falsiness instead even though it's called "truth value testing" in the documentation.
numbers list is empty:
>>> numbers = 
If we use
numbers in our
if condition again, we'll see that it doesn't evaluate as truthy this time:
>>> if numbers: ... print("yes") ... else: ... print("no") ... no
Converting an empty list to a boolean returns
>>> bool(numbers) False
Which means empty lists are are falsey in Python:
>>> not numbers True
This is true of pretty much any "empty" object.
Empty strings are falsey:
>>> name = "" >>> not name True >>> bool(name) False
In Python, empty objects are falsey. For objects that have a length, if their length is greater than zero, they're considered truthy. And objects with a length equal to zero are falsey.
Truthiness isn't not just about "having a non-zero length".
We think of Python's
None as representing emptiness.
It's kind of the ultimate representation of emptiness in fact.
So we would expect that converting
None to a boolean would give us
False, and in fact it does:
>>> bool(None) False
None is falsey.
>>> not None True
Truthiness is about two things. We've seen that truthiness is about non-emptiness. But truthiness is also about non-zeroness.
So if we check the truthiness of a number, we're asking Python is this number not equal to zero.
>>> n = 0 >>> if n: ... print("yes") ... else: ... print("no") ... no
if statements prints
If you convert
0 to a boolean you get
False, which means zero is falsey.
>>> bool(0) False
Every other number (whether negative or positive) is truthy:
>>> bool(-5) True
Every number other than
0 is truthy.
So in Python truthiness is asking the question, what happens if I convert an object to a boolean.
Every Python object is truthy by default. Any object that isn't a number, doesn't have a length, and doesn't represent emptiness in some other way is truthy.
If the object has a length which is zero, it's falsey.
If the object represents the number zero, it's falsey.
And if the object represents emptiness in some other way (like
None), it's falsey.
So truthiness in Python is about non-emptiness and non-zeroness.
The main place you'll see truthiness used is for checking non-emptiness.
For example the condition
not numbers is checking whether numbers is empty.
Hello friendly web visitor! 👋
This page is part of Python Morsels, an online Python skill-building service.
The best way to learn is by doing. In the case of Python that means writing Python code. If you'd like to improve your Python skills every week, try out Python Morsels by entering your email below to create an account.
Python Morsels topics pages are free and the first month's worth of exercises is free as well. You don't need to enter payment details to sign up.
You can find explanations of many other Python topics by signing up below.
Intro to Python courses often skip over some fundamental Python concepts.
Sign up below and I'll share ideas new Pythonistas often overlook.