Boolean Data Type in Python
In Python, the Boolean data type represents one of two possible values:
TrueFalse
Booleans are widely used in control flow, comparisons, and logical operations. They are the foundation of decision-making in any Python program.
Key Characteristics of Booleans
- Type:
bool - Possible Values:
True,False(case-sensitive) - Used in: Conditional statements, logical operations, loops, and functions.
Boolean Values in Python
boolean_values.py
is_active = True
is_closed = False
print(type(is_active))output.txt
<class 'bool'>Explanation:
TrueandFalseare Python keywords representing Boolean values.- The type of a Boolean variable is
bool.
Boolean Expressions
Boolean values often arise from expressions using comparison operators:
boolean_expressions.py
a = 10
b = 20
print(a > b)
print(a < b)
print(a == 10)output.txt
False
True
TrueExplanation:
- Comparisons (
>,<,==,!=,>=,<=) produce Boolean results. - These expressions are used in conditional logic and control flow.
Logical Operators: and, or, not
boolean_logical.py
x = True
y = False
print(x and y) # True only if both are True
print(x or y) # True if at least one is True
print(not x) # Inverts the Boolean valueoutput.txt
False
True
FalseExplanation:
| Operator | Description |
|---|---|
and | Returns True if both operands are True |
or | Returns True if at least one is True |
not | Reverses the Boolean value |
Example 1: Booleans in Conditional Statements
boolean_if.py
is_logged_in = True
if is_logged_in:
print("Welcome back!")
else:
print("Please log in.")output.txt
Welcome back!Explanation:
- Booleans control which branch of an
ifstatement is executed.
Example 2: Boolean Conversion (bool())
Any Python object can be converted to a Boolean using bool():
boolean_conversion.py
print(bool(0)) # False
print(bool(1)) # True
print(bool("")) # False (empty string)
print(bool("Python")) # True (non-empty string)output.txt
False
True
False
TrueRules:
| Value | Boolean Result |
|---|---|
Zero (0, 0.0) | False |
Empty collections ("", [], {}) | False |
None | False |
| All other values | True |
Example 3: Using Booleans in Loops
boolean_while_loop.py
counter = 0
while counter < 3:
print("Counter is:", counter)
counter += 1output.txt
Counter is: 0
Counter is: 1
Counter is: 2Explanation:
- The condition
counter < 3returns a Boolean. - The loop runs while this condition is
True.
Example 4: Combining Comparisons with Logical Operators
boolean_combined.py
score = 85
passed = score >= 50 and score <= 100
print("Passed:", passed)output.txt
Passed: TrueExplanation:
- Multiple comparisons can be combined using
and,orto form complex conditions.
Summary
- The Boolean data type represents logical values:
TrueandFalse. - Booleans are used in comparisons, conditional logic, and loops.
- Logical operators (
and,or,not) help combine or modify conditions. - Non-Boolean types can be converted using
bool()based on Python’s truthy/falsy rules.
Next, we will explore the Binary Data Types in Python — used for working with binary data like files and byte streams.