Skip to Content
Course content

17: Booleans and Truthiness

Click on the "Edit" button in the top corner of the screen to edit your slide content.

A few years ago, I was reviewing a PR for a junior dev who was building a leaderboard for a gaming app. He had written a line of code that looked like this: if not user_score:. His intention was to check if the user had ever played a game—essentially, "if the score is missing, show the 'Welcome' screen." It worked perfectly for new users. But then, we hit production, and users who had played the game but happened to have a score of exactly 0 were being kicked back to the "Welcome" screen over and over. Python saw the integer 0 and decided it was "False." It was a frustrating bug to track down because, logically, 0 is a value, but in the world of Python truthiness, it's an empty void.

The Magic of Truthiness

In most languages, a boolean is strictly True or False. Python is more flexible—and occasionally more dangerous. Every single object in Python has an inherent "truthiness." This means that when you put any object into an if statement or a while loop, Python doesn't just look for the literal keyword True; it evaluates whether that object "feels" true.

I usually tell people to think of it this way: most things are True unless they are empty or zero. If you want to see how Python views a specific value, you can wrap it in the bool() function. Try it out with a few things; you'll find that a string with a single space " " is True, but an entirely empty string "" is False.

# These all evaluate to True
print(bool("Hello"))  # Non-empty string
print(bool([1, 2]))    # Non-empty list
print(bool(42))       # Non-zero number

# These all evaluate to False
print(bool(0))        # Zero
print(bool(""))       # Empty string
print(bool([]))       # Empty list
print(bool(None))     # The None object

The "Falsy" Hall of Fame

To master Python, you have to memorize the "Falsy" list. If it's not on this list, it's True. The most common falsy values you'll encounter are None, False, the number 0 (including 0.0), and any empty sequence or collection: empty strings, lists, tuples, dictionaries, and sets.

This is where the power of "Pythonic" code comes from. Instead of writing if len(my_list) > 0:, you can simply write if my_list:. It reads like English: "If the list exists and has stuff in it, do this." It's cleaner, faster to read, and it's how experienced Python devs write their logic.

Avoiding the Truthiness Trap

While implicit truthiness is great, it's where that leaderboard bug I mentioned comes from. The danger arises when 0 or "" are actually valid pieces of data in your program. If you specifically need to know if a variable is None (meaning it was never assigned) versus whether it's 0 (meaning it was assigned a value of zero), you cannot rely on truthiness.

In those cases, you must be explicit. Use the is keyword to check for identity. I've seen countless bugs solved simply by changing if not value: to if value is None:. One checks "is this falsy?" while the other checks "is this specifically the None object?" It's a small distinction that separates a buggy script from professional software.




📋 Practical Task

Building a User Profile Validator

You are building a registration system. You need to write a function called validate_profile(username, bio, age) that checks if a user's profile is complete enough to be published. However, you have to be careful with how you handle the data!

Requirements:

  • The username must be provided (it cannot be an empty string or None).
  • The bio is optional. If it's an empty string or None, that's fine—it shouldn't block the profile.
  • The age must be provided. Since a user could technically be 0 years old (an infant), you must explicitly check that age is not None. Using if not age: will fail for infants!

Write the function so it returns True if the profile is valid and False otherwise. Test it with these cases:

# Should be True
validate_profile("coder123", "I love Python!", 25) 
# Should be True (bio is empty, age is 0)
validate_profile("baby_shark", "", 0) 
# Should be False (username is empty)
validate_profile("", "I am here", 30) 
# Should be False (age is None)
validate_profile("dev_guy", "Hello", None) 
Rating
0 0

There are no comments for now.

to be the first to leave a comment.