Skip to content
Home / Fundamentals

# Python Booleans

In Python, a boolean is a data type that can have one of two values: True or False. These values are often used to represent the truth value of an expression and are commonly used in control statements, such as if statements, to determine the flow of a program based on whether a condition is True or False. In this tutorial, we will learn about boolean values and how other data types can be used as booleans in Python.

## Boolean Values

In Python, a boolean is a data type that can have one of two values: True or False. These values are often used to represent the truth value of an expression. For example:

```x = 5
y = 3
print(x > y)  # prints True
print(x < y)  # prints False
```

Boolean values are commonly used in control statements, such as if statements, to determine the flow of a program based on whether a condition is True or False.

```x = 5
y = 3
if x > y:
print("x is greater than y")
else:
print("x is not greater than y")  # prints "x is greater than y"
```

## Other Data Types as Booleans

In Python, all values are considered either True or False when used in a boolean context. Some examples of values that are considered False in a boolean context are:

• None
• False
• Zero of any numeric type (e.g. 0, 0.0, 0j)
• Empty sequences (e.g. '', [], ())
• Empty mapping types (e.g. {})
• All other values are considered True in a boolean context.

For example:

```print(bool(None))  # prints False
print(bool(False))  # prints False
print(bool(0))  # prints False
print(bool(''))  # prints False
print(bool([]))  # prints False
print(bool({}))  # prints False

print(bool(1))  # prints True
print(bool('Hello'))  # prints True
print(bool([1, 2, 3]))  # prints True
print(bool({'key': 'value'}))  # prints True
```

It's important to note that these rules apply to all data types, not just boolean values. This means that you can use any value in a boolean context, and Python will automatically convert it to a boolean value based on these rules.

For example:

```x = 'Hello'
if x:
print("x is truthy")  # prints "x is truthy"

y = []
if not y:
print("y is falsy")  # prints "y is falsy"
```

## Best Practices

• Use descriptive variable names: When working with boolean values, it's important to use descriptive variable names that accurately reflect the purpose of the boolean. For example, instead of using a variable name like flag, use a name like is_admin or is_valid_input that clearly describes the purpose of the boolean.
• Avoid negating boolean values: It's often more readable to use positive boolean values (e.g. is_enabled) rather than negated boolean values (e.g. is_not_enabled). If you do need to negate a boolean value, consider using the not operator rather than using a negated variable name.
• Use is and is not for comparing boolean values: When comparing boolean values, it's best to use the is and is not operators rather than the == and != operators. This is because the is and is not operators compare the identity of the boolean values, whereas the == and != operators compare the values themselves.
• Use boolean expressions in if statements: When using boolean values in if statements, it's best to use boolean expressions rather than single boolean variables. For example, instead of writing if flag:, write if is_admin and has_permission: to clearly convey the conditions that must be met for the code in the if block to execute.
• Avoid using short-circuit evaluation: While short-circuit evaluation can be a useful optimization technique in some cases, it can also make your code more difficult to read and understand. Consider using explicit boolean expressions rather than relying on short-circuit evaluation to make your code more readable.