Truthy and Falsy Values in Python | Python Tutorials | BeingSkilled

In Python, every value can be evaluated as either True or False in a boolean context, such as inside if statements. These are known as truthy and falsy values.

Understanding these is essential for writing clean and efficient control flow logic.

Falsy Values in Python

The following values are considered falsy in Python:

  • False
  • None
  • 0, 0.0, 0j
  • Empty sequences: "", [], ()
  • Empty mappings: {}
  • set(), range(0)

Truthy Values in Python

Any value that is not falsy is considered truthy. This includes:

  • Non-zero numbers
  • Non-empty strings, lists, tuples, sets, and dictionaries
  • Custom objects (unless defined otherwise using special methods)

Example 1: Boolean Evaluation of an Integer

value = 5

if value:
    print("This is truthy")

Non-zero integers are considered truthy.

Example 2: Zero is Falsy

value = 0

if value:
    print("Truthy")
else:
    print("Falsy")

Zero is falsy, so the output will be "Falsy".

Example 3: Empty String is Falsy

name = ""

if name:
    print("Name is set")
else:
    print("Name is empty")

Empty strings are considered falsy.

Example 4: Non-Empty String is Truthy

name = "Alice"

if name:
    print("Hello,", name)

Since the string is not empty, the condition evaluates to True.

Example 5: Empty List is Falsy

items = []

if not items:
    print("The list is empty")

Empty containers are evaluated as falsy.

Example 6: None is Falsy

data = None

if not data:
    print("No data found")

None is always falsy.

Example 7: Using Truthy Values in Conditions

logged_in_users = ["admin", "editor"]

if logged_in_users:
    print("Users are logged in")

Non-empty lists evaluate to True.

Example 8: Set is Falsy When Empty

tags = set()

if not tags:
    print("No tags available")

Empty sets are falsy, so the message is printed.

Example 9: Using Truthy for Clean Code

email = "user@example.com"

if email:
    print("Email provided")

Instead of writing if email != "", Python allows clean truthy checks.

Example 10: Custom Truthy Objects

class AlwaysFalse:
    def __bool__(self):
        return False

obj = AlwaysFalse()

if obj:
    print("Truthy")
else:
    print("Falsy")

Custom objects can control their boolean behavior using __bool__().


Summary

  • Falsy values include: False, None, 0, "", [], {}, etc.
  • Anything that’s not falsy is considered truthy.
  • You can use any value in a condition — Python will evaluate it as True or False.
  • Use truthy/falsy logic to write cleaner and more Pythonic code.

Mastering truthy and falsy values helps simplify conditions and improve code readability. It's a foundational concept that appears in nearly every Python program.