Skip to content

Latest commit

 

History

History
53 lines (39 loc) · 2 KB

File metadata and controls

53 lines (39 loc) · 2 KB

Truthy and Falsy Values in Python

In Python, every object can be categorized as either truthy or falsy. While the most explicit examples are the True and False booleans, it's important to note that these booleans are essentially constants representing 1 and 0, respectively. You can use any non-zero number as truthy and zero as falsy. For instance, using 1 instead of True and 0 instead of False is completely valid.

# Using 1 as truthy and 0 as falsy
truthy_value = 1
falsy_value = 0

To determine the truthiness or falsiness of a value in Python, you can use the boolean type with parentheses.

# Checking the truthiness or falsiness of values
empty_list = []
is_empty_list_truthy = bool(empty_list)  # Evaluates to False

none_value = None
is_none_truthy = bool(none_value)  # Evaluates to False

non_empty_value = 200
is_non_empty_truthy = bool(non_empty_value)  # Evaluates to True

Examples

Consider different scenarios where truthy and falsy values play a crucial role in boolean checks.

# Example: Checking if users exist before performing an operation
users = {'Alice': 25, 'Bob': 30, 'Charlie': 22}

if users:
    for name, age in users.items():
        print(f"{name}: {age}")
else:
    print("No data found")

In this example, the code checks if the users dictionary is truthy before iterating over its items. If users is empty, set to None, or any other falsy value, it prints "No data found."

# Example: Checking truthiness of different data types
empty_dict = {}
empty_string = ''
non_empty_list = [1, 2, 3]

print(bool(empty_dict))       # Evaluates to False
print(bool(empty_string))     # Evaluates to False
print(bool(non_empty_list))   # Evaluates to True

Here, you can observe how empty dictionary and string are considered falsy, while a non-empty list is truthy. Understanding truthy and falsy values is essential for effective boolean checks in Python, especially when dealing with conditions based on the existence or emptiness of data.