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:
0(the integer zero)""(an empty string)[](an empty list)NoneFalse
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:
0'hello'''[1, 2, 3][]None
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:
- Create a file named truthy_falsy.py in your part39 folder.
- Complete the tasks and use # comments to explain your answers.
Exercise 1: Spot the Falsy
-
Task: Create five variables:
a = 0,b = "",c = [],d = None,e = "hello". -
Goal: Use
bool()to print the Boolean value of each one. Write a comment next to each print statement explaining why it is truthy or falsy.
Exercise 2: The Empty Check
-
Task: Create a variable
shopping_cart = []. -
Goal: Write an
if/elseblock that prints"You have items in your cart."if the list has items in it, and"Your cart is empty."if it does not. Test both outcomes by adding items to the list and running it again.
Exercise 3: The Space Trap
-
Task: Create a variable
username = " "(a single space). -
Goal: Write an
if/elseblock that prints"Username accepted."if the username is truthy and"Please enter a username."if it is falsy. Run it and note the result. Write a comment explaining why this could be a problem in a real application.
Exercise 4: Form Validation
-
Task: Create three variables:
name = "Sarah",email = "",phone = 0. -
Goal: Write a conditional block that checks all three
fields. If all three are truthy print
"Form submitted successfully.". If any one of them is falsy print"Please fill in all fields.". Test by filling in and clearing each field.
Exercise 5: Zero is not Nothing
-
Task: Create a variable
score = 0. -
Goal: Write an
if/elseblock that prints"Score recorded."if the score is truthy and"No score entered."if it is falsy. Run it and note the result. Write a comment explaining why using truthy and falsy to check a score could cause a bug when the score is legitimately zero.
Don't Forget to commit and Push!