Last modified: Mar 13, 2026 By Alexander Williams

Python Booleans Guide: True, False, Logic

Booleans are a fundamental data type in Python. They represent one of two values: True or False. These values are the building blocks of logic and decision-making in your code.

You will use booleans constantly. They control the flow of your programs with if statements and loops. Mastering them is essential for any Python developer.

What Are Boolean Values?

In Python, the boolean type is called bool. It is a subclass of the integer type, int. This means True has an integer value of 1 and False has an integer value of 0.

You can assign these values directly to variables.


# Assigning boolean values
is_sunny = True
is_raining = False

print(is_sunny)
print(type(is_sunny))
    

True
<class 'bool'>
    

You can also create booleans using comparison operators. These operators compare two values and return a bool result.

Boolean Operators: and, or, not

Logical operators allow you to combine multiple boolean expressions. Python provides three main logical operators: and, or, and not.

The and operator returns True only if both operands are True.

The or operator returns True if at least one operand is True.

The not operator simply inverts the boolean value.


# Using logical operators
a = True
b = False

print(a and b)  # False
print(a or b)   # True
print(not a)    # False

# Practical example
age = 25
has_license = True

can_drive = (age >= 18) and has_license
print(f"Can drive? {can_drive}")
    

False
True
False
Can drive? True
    

Truthy and Falsy Values

This is a crucial Python concept. In conditions, Python evaluates non-boolean values as either "truthy" or "falsy".

A value is "falsy" if it is considered False in a boolean context. Common falsy values include:

  • None
  • False
  • Zero of any numeric type (0, 0.0)
  • Empty sequences ("", [], ())
  • Empty mappings ({})

Everything else is considered "truthy". This allows for concise and Pythonic conditional checks.


# Truthy and Falsy evaluation
name = ""  # Empty string is falsy
if name:
    print(f"Hello, {name}")
else:
    print("Name is empty or falsy.")

shopping_list = ["apples", "bread"]  # Non-empty list is truthy
if shopping_list:
    print("List has items!")
    

Name is empty or falsy.
List has items!
    

The bool() Function

You can explicitly check if a value is truthy or falsy using the bool() function. It returns True for truthy values and False for falsy values.

This is very useful for debugging and understanding how Python evaluates different objects.


# Using the bool() function
print(bool(10))      # True (non-zero number)
print(bool(0))       # False
print(bool("Hello")) # True (non-empty string)
print(bool(""))      # False (empty string)
print(bool([1,2]))   # True (non-empty list)
print(bool([]))      # False (empty list)
print(bool(None))    # False
    

True
False
True
False
True
False
False
    

Boolean Expressions with Comparisons

Comparison operators are your primary tool for creating boolean expressions. They evaluate the relationship between two values.

Python's comparison operators are: == (equal), != (not equal), > (greater than), < (less than), >= (greater than or equal to), <= (less than or equal to).

You can chain these comparisons for more readable code.


# Comparison operators
x = 10
y = 20

print(x == y)   # False
print(x != y)   # True
print(x < y)    # True
print(x >= 10)  # True

# Chained comparison (checks if x is between 5 and 15)
print(5 < x < 15)  # True, equivalent to (5 < x) and (x < 15)
    

False
True
True
True
True

Boolean Logic in Control Flow

Booleans are the engine behind if, elif, else, while, and comprehension conditions. Understanding truthy/falsy evaluation makes your control flow elegant.

For instance, checking if a list is not empty before looping is a common pattern.


# Booleans in control flow
user_input = input("Enter a number: ")

# The condition is True if the string is non-empty and can be converted to a digit
if user_input and user_input.isdigit():
    number = int(user_input)
    if number % 2 == 0:
        print(f"{number} is even.")
    else:
        print(f"{number} is odd.")
else:
    print("Invalid or empty input.")
    

Short-Circuit Evaluation

Python uses short-circuit evaluation for and and or operators. This is a performance and safety feature.

For and: If the first operand is False, Python knows the result must be False and doesn't evaluate the second operand.

For or: If the first operand is True, Python knows the result must be True and doesn't evaluate the second operand.

This allows you to write code like if my_list and my_list[0] == 'key': safely.


# Demonstrating short-circuit evaluation
def check_value():
    print("This function was called!")
    return True

print(False and check_value())  # check_value() is NEVER called
print(True or check_value())    # check_value() is NEVER called
print(True and check_value())   # check_value() IS called
    

False
True
This function was called!
True

Conclusion

Booleans are simple yet powerful. The True and False values form the core of program logic.

Remember the key points: use comparison operators to create booleans, combine them with and/or/not, and leverage Python's truthy/falsy evaluation for clean code.

Practice using the bool() function to test values. Understand short-circuiting to write efficient conditions. With these tools, you can control your program's flow with confidence and clarity.