Truthiness
What is truthiness?
Truthiness is Python's way of treating any value as either "truthy" (like True) or "falsy" (like False).
In many programming languages, you can only use True and False when checking conditions. Python is more flexible because you can use any value, and Python will automatically treat it as truthy or falsy based on what it is.
Think of it this way: some values are "empty" or "nothing" (falsy), and everything else is "something" (truthy).
>>> name = "Alice"
>>> bool(name) # Non-empty string is truthy
True
>>> items = []
>>> bool(items) # Empty list is falsy
False
Why this matters
Truthiness is fundamental to Python programming because:
- Works with any type: You don't need to convert values to
TrueorFalsefirst - Cleaner code: Instead of writing
if value == True:, you can just writeif value: - Logical operators:
and,or, andnotwork with truthiness to create useful expressions - Default values: You can use
orto provide fallback values when something is empty or missing - Common patterns: Many Python code patterns rely on truthiness
You'll see truthiness in action when you learn about conditionals (making decisions in your code). For now, we'll focus on understanding which values are truthy and which are falsy.
Falsy values
These values are considered falsy in Python. They evaluate to False in boolean contexts:
The falsy values
False- The booleanFalseitselfNone- Python's way of saying "no value"0- The number zero (integer)0.0- The number zero (decimal/float)""- An empty string (no text)[]- An empty list (no items){}- An empty dictionary (no key-value pairs)set()- An empty set (no unique items)()- An empty tuple (no items)
There's also 0j (complex zero), but you probably won't need that until you're doing advanced math. For now, just remember that zero in any form is falsy.
# All of these are falsy
>>> bool(False)
False
>>> bool(None)
False
>>> bool(0)
False
>>> bool("")
False
>>> bool([])
False
>>> bool({})
False
Testing falsy values
You can use the bool() function to see if a value is falsy (returns False) or truthy (returns True):
>>> bool(False)
False
>>> bool(None)
False
>>> bool(0)
False
>>> bool("")
False
>>> bool([])
False
>>> bool({})
False
All of these return False because they're falsy values.
Truthy values
Everything that's not falsy is truthy. These will evaluate to True in boolean contexts:
# All of these are truthy
>>> bool(True)
True
>>> bool(1)
True
>>> bool(-1) # Negative numbers are truthy!
True
>>> bool("hello")
True
>>> bool([1, 2, 3])
True
>>> bool({"key": "value"})
True
Common truthy values
- Non-zero numbers:
1,-1,3.14,-0.5(any number that isn't zero) - Non-empty strings:
"hello"," "(even a string with just a space is truthy!) - Non-empty collections:
[1],{"key": "value"},(1,),{1}(any collection with at least one item) - Most objects: Functions, modules, and other objects are usually truthy
The key idea: if it's not empty and not zero, it's truthy!
>>> bool(1)
True
>>> bool(-1)
True
>>> bool("hello")
True
>>> bool(" ") # Even a single space is truthy!
True
>>> bool([1, 2, 3])
True
>>> bool({"key": "value"})
True
Using truthiness with logical operators
The logical operators and, or, and not work with truthiness in interesting ways. Let's see how they behave:
or - Returns the first truthy value, or the last value if all are falsy
The or operator is useful for providing default values:
>>> name = ""
>>> result = name or "Anonymous" # name is falsy, so use "Anonymous"
>>> result
"Anonymous"
>>> name = "Alice"
>>> result = name or "Anonymous" # name is truthy, so use "Alice"
>>> result
"Alice"
Think of or as saying: "Use the first value if it's truthy, otherwise use the second value."
and - Returns the first falsy value, or the last value if all are truthy
>>> "hello" and "world" # First is truthy, so return the second
"world"
>>> "" and "world" # First is falsy, so return the first (falsy value)
""
Think of and as saying: "If the first value is truthy, return the second value. Otherwise, return the first value."
not - Inverts truthiness
>>> not "" # Empty string is falsy, so not "" is True
True
>>> not "hello" # Non-empty string is truthy, so not "hello" is False
False
>>> not [] # Empty list is falsy, so not [] is True
True
The not operator flips truthy to False and falsy to True.
Common patterns
Default values with or
>>> user_name = None
>>> display_name = user_name or "Anonymous"
>>> display_name
"Anonymous"
>>> user_name = ""
>>> display_name = user_name or "Anonymous"
>>> display_name
"Anonymous"
>>> user_name = "Alice"
>>> display_name = user_name or "Anonymous"
>>> display_name
"Alice"
Filtering falsy values
You can use truthiness to filter out empty or falsy values from a list. (You'll learn more about this when you study lists and loops.)
>>> names = ["Alice", "", "Bob", None, "Charlie", ""]
>>> valid_names = [name for name in names if name] # Only keep truthy names
>>> valid_names
["Alice", "Bob", "Charlie"]
This keeps only the names that are truthy (non-empty strings), filtering out empty strings and None.
Chaining defaults
>>> value1 = None
>>> value2 = None
>>> value3 = "found"
>>> result = value1 or value2 or value3 or "default"
>>> result
"found"
The bool() function
The bool() function explicitly converts any value to a boolean:
>>> bool(0)
False
>>> bool(1)
True
>>> bool("")
False
>>> bool("hello")
True
>>> bool([])
False
>>> bool([1, 2, 3])
True
This is useful when you need an explicit boolean value rather than relying on truthiness in a conditional.
Custom truthiness (advanced)
This section covers advanced topics about creating your own classes. You can skip this for now and come back to it after you've learned about classes and object-oriented programming.
When you create your own classes, you can control how they behave with truthiness by defining special methods. This is an advanced topic, so don't worry if it seems confusing right now.
If you create a class and don't define these special methods, instances of your class will always be truthy.
Truthiness vs. explicit comparisons
Sometimes you need to be specific about what you're checking for. There's a difference between checking if something is falsy and checking if it's a specific value:
Explicit comparisons
These check for specific values:
>>> value = None
>>> value is not None # Is value specifically None? No, it IS None
False
>>> name = ""
>>> name != "" # Is name not equal to empty string? No, it IS ""
False
>>> count = 0
>>> count == 0 # Is count equal to zero? Yes
True
Truthiness evaluation
These check if something is truthy or falsy (could be multiple things):
>>> name = "Alice"
>>> bool(name) # Is name truthy? Yes (non-empty string)
True
>>> items = [1, 2, 3]
>>> bool(items) # Is items truthy? Yes (non-empty list)
True
>>> user_input = ""
>>> bool(user_input) # Is user_input truthy? No (empty string is falsy)
False
When to use which?
- Use truthiness (
bool(value)) when you want to know if something has a value (not empty, not zero, not None) - Use explicit comparisons (
value == 0,value is None) when you need to check for a specific value
When you learn about conditionals, you'll see that if value: uses truthiness, while if value is not None: uses explicit comparison. Both have their place, and you'll learn when to use each one.
Common pitfalls
Empty string vs. None
>>> name = ""
>>> bool(name) # False - empty string is falsy
False
>>> name is None # False - name is "", not None
False
>>> name = None
>>> bool(name) # False - None is falsy
False
>>> name is None # True - name is actually None
True
Zero is falsy
>>> count = 0
>>> bool(count) # False - 0 is falsy!
False
>>> count is not None # True - 0 is a valid value, just falsy
True
Remember: 0 is a valid number, but it's falsy. If you need to distinguish between "no value" and "zero", use explicit checks.
Space in strings
>>> name = " " # String with just a space
>>> bool(name) # True - non-empty string is truthy!
True
>>> bool(name.strip()) # False - stripped string is ""
False
A string containing only whitespace is truthy because it's not empty. To check for "meaningful" content, strip whitespace first.
Best practices
Here are some guidelines for working with truthiness:
- Use
bool()when you need an explicit boolean: If you need aTrueorFalsevalue specifically, usebool(value) - Be explicit when the distinction matters: Use
is Noneor== ""when you need to check for a specific value, not just whether something is falsy - Remember edge cases:
0,"", and[]are falsy—make sure that's what you want in your code - Use
orfor defaults:value or "default"is a common and useful pattern for providing fallback values
# Good: Using bool() for explicit conversion
>>> items = [1, 2, 3]
>>> bool(items)
True
# Good: Explicit when distinction matters
>>> count = 0
>>> count is not None # Distinguishes between 0 and None
True
# Good: Using truthiness for defaults
>>> user_input = ""
>>> result = user_input or "default"
>>> result
"default"
Summary
Truthiness is a powerful Python feature that makes code more readable:
- Falsy values:
False,None,0,0.0,"",[],{},set(),()— these are all "empty" or "nothing" - Truthy values: Everything else — any non-zero number, non-empty string, non-empty collection, etc.
- The
bool()function: Converts any value to an explicitTrueorFalse - Logical operators:
and,or, andnotwork with truthiness to create useful expressions - Default values: Use
orto provide fallback values when something is falsy (e.g.,name or "Anonymous") - Best practices: Use
bool()for explicit conversion, be explicit when you need to check for specific values
Understanding truthiness helps you write cleaner, more readable code. You'll use this knowledge extensively when you learn about conditionals (making decisions in your code)!