Flat_Eric Contemplating Data Type Conversions

Truthy vs Falsy

What the...?

Truthy and Falsy

In Python, every value has an implicit Boolean value. We call these truthy and falsy. A truthy value is any value that Python treats as True in a condition, and a falsy value is any value that Python treats as False.

This is different from the actual Boolean values True and False. Those are explicit, you are directly telling Python the answer. Truthy and falsy are implicit, Python looks at the value and decides for itself.

The following values are considered falsy in Python:

Everything else is considered truthy. A non-zero number, a string with text in it, a list with items in it. Python will treat all of these as True in a condition.

Truthy and Falsy in Action

In the previous example we used True and False directly. Below, we have replaced is_enrolled with a string. Python will evaluate whether that string is truthy or falsy and use that as the condition.

Try changing is_enrolled to an empty string "" and run it again to see the difference.

Behind the scenes, Python is checking the data type using bool(). When Python evaluates a condition, it is essentially wrapping the value in a bool() call and asking "does this return True or False?"

Checking the bool Value

You can check the truthy or falsy value of anything by wrapping it in bool(). This is exactly what Python is doing internally when it evaluates your conditions.

The box below contains one example to get you started. Try replacing the value inside bool() with each of the following and run it each time to see what Python returns:

Real World Use: Form Validation

One of the most common real world uses of truthy and falsy is checking whether a user has filled in a form. Instead of checking if a value is explicitly True, we can simply check if it exists.

Think about a login form. If the user submits the form without filling in their username or password, those fields will be empty strings. An empty string is falsy, so Python will treat them as False. If the fields are filled in, the values are truthy and Python will treat them as True.

Try setting username or password to an empty string "" and run it again to simulate a user leaving a field blank. What happens if you just enter a "space"?

A single space " " is considered truthy. An empty string "" is falsy because it contains nothing at all. A space is a character, and a string with any character in it has a value, so Python treats it as truthy. This means a user could technically submit a single space as a password and pass the check, which is why real applications add extra validation on top of this.

Time to experiment!

Coding Exercises (VS Code) Instructions:

Exercise 1: Spot the Falsy

Exercise 2: The Empty Check

Exercise 3: The Space Trap

Exercise 4: Form Validation

Exercise 5: Zero is not Nothing

Don't Forget to commit and Push!

This course was built by DevSTEM - we turn teaching materials into interactive web courses like this one.

Build your own course