Skip to main content
MahiDev — Learn. Build. Become AI-ready.

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.

Beginner12 min

Visual reference

Light switch — on or off, like True and FalseLight switch — on or off, like True and False
Python literals — including boolean valuesPython literals — including boolean values
Boolean logic in Python programsBoolean logic in Python programs

What you will learn

  • What is bool? — Python’s logical data type
  • True and False — 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
Decision flows to True or 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

python
is_logged_in = True
is_admin = False

print(is_logged_in)
print(type(is_logged_in))
text
True
<class 'bool'>

Memory tip: capitalization matters

Correct Wrong
True, False true, false

Lowercase names are treated as variables:

python
# a = true
# NameError: name 'true' is not defined

The hidden secret: bool is a subtype of int

In Python’s execution environment, True is treated as 1 and False as 0.

True maps to 1, False maps to 0

Verify:

python
print(int(True))
print(int(False))
text
1
0

bool 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
python
print(True + False)
print(True + True)
print(False + False)
text
1
2
0

Interactive: 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).

python
print(True + 0b1010)
print(True - False + 0xF)

Example 1: True + 0b1010 → 1 + 10 = 11

Example 2: True - False + 0xF → 1 - 0 + 15 = 16

text
11
16

Also from your notes:

python
print(2 * True + 3)    # 5
print(2 * (True + 3))  # 8 — True becomes 1 before (True + 3)

Multiplication step by step

Evaluating 2 × True + 3
python
print(2 * True + 3)
text
5

Division: 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
python
print(True / True)
print(False / True)
# print(False / False)  # ZeroDivisionError

False is 0, so False / False is division by zero.

Why is bool useful in programming?

Booleans are mainly for conditions, not clever math.

python
is_logged_in = True

if is_logged_in:
    print("Welcome!")
if is_logged_in flow

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:

python
if is_admin:
    access_dashboard()

Avoid:

python
if is_admin == True:
    ...

Use booleans to express intent, not clever math.

Practice exercises

  1. Predict True + True and False // True before running in the playground.
  2. Uncomment False / False once to see ZeroDivisionError.
  3. Set is_logged_in = False and run the if block — what prints?

Quick knowledge check

  1. Valid boolean keywords?
    True and False (capitalized).

  2. What is True + False?
    1.

  3. Why does False / False fail?
    Division by zero (0 / 0).

Summary

  • bool stores only True and False (logical values).
  • Keywords must be capitalized.
  • Internally True = 1, False = 0; bool is a subtype of int.
  • Arithmetic on booleans works; if is the main real-world use.
  • False / False and True / False raise ZeroDivisionError.

Next lesson: complex — complex numbers, j notation, and complex arithmetic.

Loading playground…