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?"
— ShurAIWhat 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.
# 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'>
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 to | 5 == 5 | True |
| != | Not equal to | 5 != 3 | True |
| > | Greater than | 10 > 3 | True |
| < | Less than | 10 < 3 | False |
| >= | Greater or equal | 5 >= 5 | True |
| <= | Less or equal | 3 <= 2 | False |
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:
T and F → False
F and F → False
F or T → True
F or F → False
not False → True
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):
0 and 0.0
"" (empty string)
[] (empty list)
None
Any non-zero number
Any non-empty string
Any non-empty list
Any object
# 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
# 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?