Lesson 03
bool — Boolean Data Type
Master True and False, understand why they behave like 1 and 0, and learn how booleans power decisions in Python.
Visual reference
What you will learn
- What is
bool? — Python’s logical data type TrueandFalse— the only two boolean values- 1 and 0 relationship — why booleans work in arithmetic
- Real-world conditions — decisions using
if
Big picture: why do we need bool?
Computers constantly make decisions. Every decision eventually becomes one of two answers:
- Yes →
True - No →
False
Real-life examples
| Question | If True |
If False |
|---|---|---|
| User logged in? | Open dashboard | Show login |
| Payment successful? | Confirm order | Show error |
| Internet available? | Sync data | Offline mode |
| Notifications enabled? | Send alert | Skip alert |
What is bool?
bool is a pre-defined class and a fundamental type.
Purpose: store logical values — only True and False.
True |
False |
|---|---|
| Yes, on, success, enabled | No, off, failure, disabled |
Analogy
Think of a light switch: ON = True, OFF = False. There is no third stable state.
Creating boolean variables
is_logged_in = True
is_admin = False
print(is_logged_in)
print(type(is_logged_in))True
<class 'bool'>Memory tip: capitalization matters
| Correct | Wrong |
|---|---|
True, False |
true, false |
Lowercase names are treated as variables:
# a = true
# NameError: name 'true' is not definedThe hidden secret: bool is a subtype of int
In Python’s execution environment, True is treated as 1 and False as 0.
Verify:
print(int(True))
print(int(False))1
0bool is a special numeric type that behaves like integers when needed.
Boolean arithmetic
Since True = 1 and False = 0, arithmetic on booleans is arithmetic on 0 and 1.
| Expression | Calculation | Answer |
|---|---|---|
True + False |
1 + 0 | 1 |
True + True |
1 + 1 | 2 |
False + False |
0 + 0 | 0 |
2 * True + 3 |
2×1 + 3 | 5 |
print(True + False)
print(True + True)
print(False + False)1
2
0Interactive: try expressions in the playground
Scroll to Try it in the playground at the bottom. Edit expressions like True + False or 2 * True + 3, run again, and watch Python treat True as 1 and False as 0.
Mixing bool with integers
Booleans combine with binary, octal, and hex integers too (from your notes).
print(True + 0b1010)
print(True - False + 0xF)Example 1: True + 0b1010 → 1 + 10 = 11
Example 2: True - False + 0xF → 1 - 0 + 15 = 16
11
16Also from your notes:
print(2 * True + 3) # 5
print(2 * (True + 3)) # 8 — True becomes 1 before (True + 3)Multiplication step by step
print(2 * True + 3)5Division: be careful
| Expression | Calculation | Result |
|---|---|---|
True / True |
1 ÷ 1 | 1.0 |
False / True |
0 ÷ 1 | 0.0 |
False // True |
0 // 1 | 0 |
False / False |
0 ÷ 0 | Error |
True / False |
1 ÷ 0 | Error |
print(True / True)
print(False / True)
# print(False / False) # ZeroDivisionErrorFalse is 0, so False / False is division by zero.
Why is bool useful in programming?
Booleans are mainly for conditions, not clever math.
is_logged_in = True
if is_logged_in:
print("Welcome!")Enterprise-style flags
| Boolean variable | Meaning |
|---|---|
is_authenticated |
User logged in? |
is_admin |
Admin privileges? |
email_verified |
Email confirmed? |
payment_success |
Payment completed? |
is_deleted |
Soft-delete flag |
Best practice
Boolean arithmetic is valid in Python, but avoid using it for business logic — it hurts readability.
Recommended:
if is_admin:
access_dashboard()Avoid:
if is_admin == True:
...Use booleans to express intent, not clever math.
Practice exercises
- Predict
True + TrueandFalse // Truebefore running in the playground. - Uncomment
False / Falseonce to seeZeroDivisionError. - Set
is_logged_in = Falseand run theifblock — what prints?
Quick knowledge check
-
Valid boolean keywords?
TrueandFalse(capitalized). -
What is
True + False?
1. -
Why does
False / Falsefail?
Division by zero (0 / 0).
Summary
boolstores onlyTrueandFalse(logical values).- Keywords must be capitalized.
- Internally
True = 1,False = 0;boolis a subtype ofint. - Arithmetic on booleans works;
ifis the main real-world use. False / FalseandTrue / FalseraiseZeroDivisionError.
Next lesson: complex — complex numbers, j notation, and complex arithmetic.
Loading playground…