In Python, a Boolean (or bool) represents one of two values: True or False. Booleans are commonly used for decision-making and control flow in programs.
Key Features of Booleans
- Binary Nature: Booleans have only two values: True and False.
- Type: The Boolean type is a subclass of the integer type (int), where True is equivalent to 1 and False is equivalent to 0.
- Logical Operations: Booleans are primarily used with logical operators (and, or, not).
Defining Boolean Values
Booleans are defined directly as True or False (case-sensitive):
is_active = True
is_closed = False
Boolean as Results of Expressions
Booleans are often returned as results of comparisons or logical operations:
# Comparison operators
print(5 > 3) # True
print(10 == 20) # False
print(15 != 10) # True
# Logical operators
print(True and False) # False
print(True or False) # True
print(not True) # False
Boolean as Integers
Booleans can behave like integers (True as 1, False as 0):
# Arithmetic operations
print(True + True) # 2
print(False * 10) # 0
# Checking type
print(isinstance(True, int)) # True
Type Conversion to Boolean
You can convert other data types to Boolean using the bool() function. Most values are considered True, except for those that are defined as falsy.
Truthy Values
Any non-zero number, non-empty string, list, tuple, dictionary, set, or other objects.
print(bool(1)) # True
print(bool("hello")) # True
print(bool([1, 2, 3])) # True
Falsy Values
The following values evaluate to False:
- None
- False
- 0 (any numeric zero)
- Empty sequences or collections (“”, [], (), {}, set())
print(bool(0)) # False
print(bool("")) # False
print(bool(None)) # False
Logical Operators
Operator | Description | Example | Result |
and | Returns True if both operands are True. | True and False | FALSE |
or | Returns True if at least one operand is True. | True or False | TRUE |
not | Reverses the Boolean value. | not True | FALSE |
Common Use Cases of Booleans
- Conditional StatementsBooleans control the flow of if and while statements:
is_raining = True if is_raining: print("Take an umbrella!") else: print("No umbrella needed.")
- Loop Control
while True: # Infinite loop print("This will run forever!") break # Stops the loop
- Boolean Checks in Functions
def is_even(num): return num % 2 == 0 print(is_even(4)) # True print(is_even(5)) # False
- Short-Circuit EvaluationPython uses short-circuit evaluation to stop evaluating expressions as soon as the result is known:
print(False and expensive_function()) # Does not call `expensive_function` print(True or expensive_function()) # Does not call `expensive_function`
Boolean Pitfalls
- Case Sensitivity: True and False must be capitalized. Writing true or false will cause a NameError.
print(True) # Valid print(true) # Error
- Implicit Boolean Contexts: Many Python constructs automatically convert values to Boolean in contexts like if statements or while loops.
value = 0 if value: print("Value is truthy!") else: print("Value is falsy!") # Output: Value is falsy!
Summary
- Boolean Values: True and False.
- Comparison Results: Return Boolean values.
- Logical Operators: Combine or invert Boolean expressions.
- Type Conversion: Use bool() to convert other types to Boolean.
- Control Flow: Booleans are central to decision-making and loop control.