Introduction
George Boole was an English mathematician who is credited with having invented a branch of algebra in which an expression is evaluated to one of the truth values, true or false. Unsurprisingly, this became known as Boolean algebra (I’m hoping that Thompsonian snark will be a thing in the not too distant future).
This post looks at how all objects in Python can be tested for a truth value and what that means.
True dat
In Python, you can assign True or False to any variable.
1 2 3 4 5 6 7 8 |
# Assign True to a, and False to b a = True b = False a True b False |
That’s all well and good and is very useful if we want to have a binary type value to assign, rather than trying to use 0’s and 1’s, ‘yes’ or ‘no’ etc., which could cause confusion with your code where you also use those values for other purposes e.g. it probably doesn’t make sense to have 5 -5 = False.
You can’t handle the truth
Additionally, in Python, any object can be tested to see if it is truthy or falsy. What does that actually mean? Take a look at the following code:
1 2 3 4 5 |
if foo == 5: bar() if foo: bar() |
A nice and simple example but for beginners to Python, the second if statement might cause confusion. Initially, let’s confirm we understand the first if statement; if the foo variable refers to the value 5, that branch runs i.e. bar() is called, if not, that branch is skipped.
What about the second example. What is if foo: doing? This is where Python tests the truthiness or falseness of the foo variable. In short, that statement means if foo is truthy, run that branch, if it is falsy, skip that branch. So bar() will only be called if foo is truthy.
The truth will set you free
So what determines if something in Python is truthy or falsy? It depends on the type being evaluated. The table below lists the main types within Python. Remember that these are all objects in Python. (Some other languages consider data types such as strings and tuples to be primitives.)
Type | When is it truthy? | When is it falsy? |
str | Not empty | Empty, ” |
list | Not empty | Empty, [] |
tuple | Not empty | Empty, () |
set | Not empty | Empty, set() |
dict | Not empty | Empty, {} |
int | Not zero, 0 | Zero, 0 |
float | Not 0.0 | 0.0 |
bool | True | False |
NoneType | Never | Always |
Function | Always | Never |
Class | Always | Never |
Let’s go back to that second if statement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# If any one of the following assignments are used, with the others # commented out, 'if foo:' will evaluate to True, based on the # truthiness of foo and so that branch will be executed foo = 'spam' # str foo = ['spam', 'eggs'] # list foo = (spam, eggs) # tuple foo = {5, 4, 3, 2, 1}# set foo = {'dead': 'parrot'} # dict foo = 42 # int foo = 3.14 # float foo = True # bool # Function and class examples shown later if foo: bar() # The difference between something being truthy and something being # True should be pointed out here for further clarity. The syntax # below checks that foo is a Boolean value, True if foo is True: # However, the syntax below checks if foo tests as being truthy. So # that not only includes foo = True, but also foo = 'hello', # foo = 15 and so on as per the table above. if foo: # PLEASE NOTE, PEP8 PREFERS THE if foo: SYNTAX IN ANY CASE |
There is a really handy built-in function we can use to tell us if something is truthy or falsy, the bool() function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
foo = 5 bool(foo) True foo = 0 bool(foo) False # To test the other types in the table, we need to # create a function and class def test: pass bool(test) True class Test: pass bool(Test) True # And finally, the NoneType, which will always test as False bool(None) False |
Summary
This post began discussing the True and False Boolean values you can assign in Python and then went on to discuss how all objects in Python can be tested for a truth value and what that means. I hope this has been useful.
Till the next time.