What Is a Boolean? The On/Off Switch Inside Every If Statement
On or off. True or false.
That's it. That's what a Boolean is.
It sounds almost too simple to matter — but Booleans are the single most important concept in all of programming. Every if statement, every loop condition, every decision your code ever makes comes down to one of exactly two values: True or False.
If you want to see the idea in 60 seconds first, the Booleans Short has you covered. Then come back here for the full walkthrough.
The Light Switch in Your Code
Imagine a light switch. It's either up (on) or down (off). There's no in-between, no dimmer setting, no "sort of on".
A Boolean works exactly the same way.
light_on = True
door_locked = False
Python stores these as one of exactly two values — always with a capital letter: True or False. Lowercase true and false aren't the same thing. Python will throw a NameError. Capitals matter.
How Booleans Power Your If Statements
The moment a Boolean meets an if, something happens:
game_over = False
if game_over:
print("Game over!")
else:
print("Keep playing!")
Python reads if game_over and checks the switch. Is it True? Run the first block. Is it False? Skip to else.
This is happening inside every piece of code you've ever run. Buttons, menus, login checks, game states — all Booleans under the hood.
Combining Booleans: and, or, not
Three words that let you ask more complex questions.
and — both must be true
has_key = True
door_unlocked = True
if has_key and door_unlocked:
print("You may enter.")
Both switches have to be on. One False and the whole thing evaluates to False.
or — at least one must be true
is_admin = False
is_owner = True
if is_admin or is_owner:
print("Access granted.")
Either switch will do. The only way to get False from or is False or False.
not — flip it
game_paused = False
if not game_paused:
update_player_position()
not reverses the value. not False is True. not True is False. Use it when you want to read "if this is NOT the case" — it makes conditions feel like plain English.
The Mistake That Catches Everyone
This one's sneaky:
answer = "False"
if answer:
print("This runs!")
Wait — "False" is True?
Yes. Because "False" is a string — a piece of text. Any non-empty string is truthy in Python, even if the content spells out the word "False".
If you want the actual Boolean False, don't put it in quotes:
answer = False # Boolean — the switch is off
answer = "False" # String — this is text, not a switch
The rule: quotes make text. No quotes makes a Boolean. They look similar on screen and behave completely differently.
Quick Reference
| Expression | Result |
|---|---|
True and True |
True |
True and False |
False |
False and False |
False |
True or False |
True |
False or False |
False |
not True |
False |
not False |
True |
bool("False") |
True (non-empty string!) |
bool("") |
False (empty string) |
Try It Yourself
Open Python and run this:
print(True and False)
print(True or False)
print(not True)
print(bool("False"))
print(bool(""))
Work through each line before you run it. Predict what you'll see. Then check whether you were right.
That process — predict, run, compare — is how you build real intuition for how Booleans work, faster than any amount of reading.