Transcript
Let's talk about truthiness in Python.
If statements check truthiness
We have a list of numbers:
>>> numbers = [2, 1, 3, 4, 7]
and we have an if
statement that either prints yes
or no
based on the condition "numbers
":
>>> if numbers:
... print("yes")
... else:
... print("no")
...
yes
Note that this condition isn't checking len(numbers) == something
or some other expression involving numbers
. The condition is just "numbers
"; that's it!
We got yes
when we evaluated that if
statement, despite the fact that numbers
is not True
:
>>> numbers == True
False
The numbers
variable points to a list, but True
is a boolean.
But if we convert numbers
to a boolean we will get True
:
>>> bool(numbers)
True
Python is checking the truthiness of numbers
. Truthiness is about asking the question: if we converted this object to a boolean, what would we get?
Python's 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.
Boolean expressions check truthiness
In fact, all boolean expressions check for truthiness.
If we use the not
operator on numbers
:
>>> not numbers
This will implicitly convert numbers
to a boolean and then negate whatever we get back (as if we said not bool(numbers)
):
>>> not numbers
False
So not numbers
is really checking the falsiness of numbers
(where falsiness is the opposite of truthiness).
The documentation calls this "truth value testing"
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.
Empty objects are falsey
This 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 False
:
>>> 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 is about non-emptiness
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
So None
is falsey.
>>> not None
True
Truthiness is also about non-zeroness
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
That if
statements prints no
because n
is 0
.
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.
Summary
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.
from Planet Python
via read more
No comments:
Post a Comment