Truth Value Testing
Here’s a code snippet showing the example code used in this lesson:
true_value = "aaa" # truthy
false_value = [] # falsey
none_value = None
# Bad:
# if true_value == True:
# print("truthy")
# if false_value == False:
# print("falsy")
# Good:
if true_value:
print("truthy")
if not false_value:
print("falsy")
if none_value is None:
print(None)
00:00
In this lesson, I want to talk to you about testing for truth values in Python. So, you might want to do something like writing if true_value == True:
and then print something out, and that makes sense, but only if the value that you’re checking for is actually going to be specifically true, so the Boolean value True
.
00:20 But there’s also other values that are truthy in Python. For example, you have numbers that are non-zero or non-empty strings or non-empty lists. Any of these would be considered truthy, but they would not catch with this statement. Let me show you this.
00:36
If I say true_value
equals the Boolean value True
and false_value
equals the Boolean value False
, these two statements are now going to run as expected.
00:46 I also have another one down here. Let me comment that out.
00:50
So now it’s just checking for the Boolean truth values True
and False
, so when I execute this,
00:58
then these two print statements—the conditional statements execute, and the print()
functions, they run as expected and print out the results. However, if I change anything of this for another truthy value or a false value here—an empty list would be falsy in Python—
01:16
then these checks are not going to work anymore. If I run this same script now, I don’t get any output. This is because you’re explicitly testing for the Boolean value True
. Now, very often, you want to just test whether a value is truthy.
01:30 This one would be considered truthy,
01:34 and this one would be considered falsy, for example. And if you want to make sure that your checks—your truth value checks—would also work in that case, what you can do instead of this is you can write your code like this.
01:49
You can just say if true_value:
and that implicitly tests for whether the value is truthy, and if not false_value:
implicitly tests whether it is falsy. So if I run the script now, we’ll see that again I get the expected output when these conditional statements execute.
02:08
So, this is generally the better way of testing for truth values in Python because it catches more truthy and falsy values apart from just the Boolean True
and False
.
02:19
This is preferred unless you specifically want to check for the Booleans. Now, another similar thing is if we want to test for None
, you should write it in this way that you use the is
operator to check for the identity of the None
object.
02:35
You have always just one None
object in your running Python session, and you always want to check for that one instead of using the double equals sign (==
).
02:44
You don’t want to do that. But instead, just use the is
operator if you’re checking for None
. So these three statements would be a good way of writing your truth value testing in a Pythonic, idiomatic way.
Become a Member to join the conversation.