bool Fundamentals¶
The bool type represents Boolean values, which model logical truth.
Python has exactly two Boolean values:
True
False
````
These values are fundamental to:
* decision making
* control flow
* comparisons
* logical expressions
```mermaid
flowchart TD
A[bool]
A --> B[True]
A --> C[False]
1. Boolean Values¶
A Boolean value answers a yes-or-no question.
Examples:
is_raining = True
is_finished = False
A program often uses Boolean variables to represent conditions.
if is_raining:
print("Take an umbrella")
2. Type of Boolean Values¶
We can inspect the type using type().
print(type(True))
print(type(False))
Output:
<class 'bool'>
<class 'bool'>
3. bool as a Subclass of int¶
In Python, bool is a subclass of int.
This means:
print(True == 1)
print(False == 0)
Output:
True
True
And arithmetic is possible:
print(True + True)
print(True + False)
Output:
2
1
flowchart LR
A[int] --> B[bool]
B --> C[True = 1]
B --> D[False = 0]
This behavior is sometimes useful, but it can also be confusing if misunderstood.
4. Creating Boolean Values¶
Boolean values are often produced by comparisons.
print(3 > 2)
print(10 == 5)
Output:
True
False
They can also be created using bool().
print(bool(1))
print(bool(0))
Output:
True
False
5. Booleans in Control Flow¶
Boolean values are central to control flow.
logged_in = True
if logged_in:
print("Welcome back")
else:
print("Please log in")
The if statement depends on whether the condition is true or false.
6. Worked Examples¶
Example 1: simple condition¶
is_sunny = True
if is_sunny:
print("Go outside")
Output:
Go outside
Example 2: equality result¶
x = 5
y = 5
print(x == y)
Output:
True
Example 3: arithmetic with bool¶
print(True + 3)
Output:
4
7. Common Pitfalls¶
Forgetting that bool is numeric¶
Because True and False behave like 1 and 0, arithmetic expressions may produce surprising results.
Using == True unnecessarily¶
Instead of:
if is_ready == True:
...
prefer:
if is_ready:
...
8. Summary¶
Key ideas:
boolhas exactly two values:TrueandFalse- Boolean values represent logical truth
boolis a subclass ofint- Booleans are produced by comparisons and used in control flow
The bool type is the foundation of logical reasoning in Python programs.