Course Progress11% complete
🐍 Python Basics Topic 11 / 100
⏱ 8 min read

Booleans

True and False — the foundation of all logic in programming. How Python makes every decision.

"Every decision a program makes — every if, every loop, every filter — comes down to one question: True or False?"

— ShurAI

What is a Boolean?

A boolean is the simplest possible type of data. It has exactly two possible values: True or False. Named after mathematician George Boole, booleans are the foundation of every decision a computer makes.

Every time your program asks a question — "Is the user logged in?", "Is the score high enough?", "Is the file found?" — the answer is always a boolean.

True
Yes / On / Correct / Exists
False
No / Off / Incorrect / Missing
python
# Boolean variables — always capitalised exactly
is_logged_in   = True
is_admin       = False
has_paid       = True
is_weekend     = False

print(type(True))    # <class 'bool'>
print(type(False))   # <class 'bool'>
Capitalisation is Required

True and False must always be capitalised exactly. Writing true or false in lowercase causes a NameError — Python does not recognise them.

Comparison Operators — They Return Booleans

When you compare two values, Python evaluates the comparison and gives back a True or False result:

Operator Meaning Example Result
==Equal to5 == 5True
!=Not equal to5 != 3True
>Greater than10 > 3True
<Less than10 < 3False
>=Greater or equal5 >= 5True
<=Less or equal3 <= 2False
python
age   = 20
score = 85

print(age >= 18)      # True  — is an adult
print(score == 100)   # False — not perfect score
print(score != 0)     # True  — not zero
print(age < 18)       # False — not a minor

Logical Operators — and, or, not

Combine multiple boolean conditions using three logical operators:

and
Both must be True to get True
T and T → True
T and F → False
F and F → False
or
At least one True gives True
T or F → True
F or T → True
F or F → False
not
Flips True↔False
not True → False
not False → True
python
age     = 22
has_id  = True
is_vip  = False

# and — both conditions must be True
print(age >= 18 and has_id)    # True

# or — at least one condition True
print(is_vip or age >= 18)    # True — second condition saves it

# not — flip the result
print(not is_vip)               # True — flipped False to True
print(not has_id)               # False — flipped True to False

Truthy and Falsy Values

In Python, every value behaves like a boolean when used in a condition. Some values are treated as Falsy (like False) and everything else is Truthy (like True):

❌ Falsy — treated like False
False
0 and 0.0
"" (empty string)
[] (empty list)
None
✅ Truthy — treated like True
True
Any non-zero number
Any non-empty string
Any non-empty list
Any object
python
# bool() converts any value to True or False
print(bool(0))       # False
print(bool(42))      # True
print(bool(""))      # False — empty string
print(bool("Hi"))    # True
print(bool(None))    # False
print(bool([]))      # False — empty list
print(bool([1,2]))  # True  — non-empty list

Real Example — Access Control System

python
# Simple login access check using booleans
username       = "admin"
password_ok    = True
account_active = True
is_banned      = False

can_login = password_ok and account_active and not is_banned

if can_login:
    print(f"Welcome, {username}! Access granted.")
else:
    print("Access denied. Check your credentials.")

# Output: Welcome, admin! Access granted.

"Booleans are simple but mighty. Every if-statement, every loop condition, every filter traces back to one small True or False."

— ShurAI

🧠 Quiz — Question 1

What are the only two values a boolean can have in Python?

🧠 Quiz — Question 2

What does 10 > 5 and 3 < 2 evaluate to?

🧠 Quiz — Question 3

What does bool("") return?

🧠 Quiz — Question 4

What does not True evaluate to?