Python
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax and Data Types
-
Section 3: Collections
-
39: Set Operations: Union, Intersection, Difference
-
Section 4: Control Flow
-
Section 5: Functions
-
Section 6: Turtle Graphics and Early Practice Projects
-
Section 7: Working with Files and I/O
-
Section 8: Regular Expressions
-
Section 9: Object-Oriented Python
-
Section 10: Error Handling
-
Section 11: Modules and Packages
-
Section 12: Iterators, Generators, and Functional Tools
-
Section 13: Decorators and Metaprogramming
-
Section 14: Concurrency and Parallelism
-
Section 15: Working with Dates, Times, and Numbers
-
Section 16: Standard Library Deep Dive I: Data Structures
-
Section 17: Standard Library Deep Dive II: System and Introspection
-
Section 18: Standard Library Deep Dive III: Security and Encoding
-
Section 19: Standard Library Deep Dive IV: Text and Data Utilities
-
Section 20: Networking and Web Basics
-
Section 21: Working with Databases
-
Section 22: Testing and Quality
-
Section 23: Advanced Typing
-
Section 24: Context Managers and Resource Handling
-
Section 25: Text, Unicode, and Binary Data
-
Section 26: More Functional and Iteration Tools
-
Section 27: Data Validation and Configuration
-
Section 28: Working with Images and Media
-
Section 29: Property-Based and Documentation Testing
-
Section 30: Packaging and Deployment
-
Section 31: Performance and Internals
-
Section 32: Design Patterns in Python
-
Section 33: GUI Programming
-
Section 34: Security Basics
-
Section 35: Data Structures and Algorithms
-
Section 36: Practical Projects
-
Section 37: Capstone Projects
-
Section 38: Interview and Algorithm Practice
-
Section 39: Writing Idiomatic Python
17: Booleans and Truthiness
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
usernamemust be provided (it cannot be an empty string orNone). - The
biois optional. If it's an empty string orNone, that's fine—it shouldn't block the profile. - The
agemust be provided. Since a user could technically be0years old (an infant), you must explicitly check thatageis notNone. Usingif 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)
There are no comments for now.