Booleans & Logic
True, False, truthy/falsy values, logical operators, and comparison chaining.
OK so this is basically like if/else but going deeper, right? Last week we used True and False in conditions — if score >= 90: and stuff. But I feel like there's more going on.
There is. Last week you used booleans without really thinking about them. Every time you wrote if score >= 90:, Python was evaluating score >= 90 and getting back either True or False. This week, we're going to understand that machinery.
Booleans are Python's truth type. There are only two values: True and False. They're the result of every comparison, every condition check, every logical operation.
print(10 > 5) # True
print(10 == 5) # False
print(type(True)) # <class 'bool'>
Here's a fun fact: booleans are a subtype of integers. True is 1, False is 0. This means you can do math with them — which turns out to be surprisingly useful.
print(True + True) # 2
print(False + True) # 1
Wait, True is literally the number 1? That's wild. So if I had a list of pass/fail results, I could just... sum them?
Exactly. And that's a pattern you'll use in real data work. Imagine you have a spreadsheet column of scores and you want to count how many passed. In Excel you'd use COUNTIF. In Python, you can sum the booleans:
scores = [92, 55, 78, 43, 88]
passed = sum(score >= 60 for score in scores)
print(passed) # 3
Each score >= 60 produces True (1) or False (0), and sum() adds them up.
OK, and what about things that aren't True or False? I heard Python has "truthy" and "falsy" values.
Every value in Python has a boolean identity. When you put something in an if statement, Python converts it to True or False behind the scenes.
Falsy values — these all act like False:
bool(0) # False — zero
bool(0.0) # False — zero float
bool("") # False — empty string
bool([]) # False — empty list
bool(None) # False — None
Everything else is truthy:
bool(1) # True — any non-zero number
bool(-5) # True — even negative numbers
bool("hello") # True — non-empty string
bool([1, 2]) # True — non-empty list
This is why you can write if name: instead of if name != "":. Python treats the empty string as falsy. You'll see this pattern everywhere in real code.
How do and, or, and not work? I used and once last week but I was kind of guessing.
These are logical operators. They combine boolean values:
# and — True only if BOTH sides are True
print(True and True) # True
print(True and False) # False
# or — True if EITHER side is True
print(False or True) # True
print(False or False) # False
# not — flips the value
print(not True) # False
print(not False) # True
In practice, you combine comparisons. Think about the business rules you'd write in a spreadsheet — "approve if age is 18+ AND income is 30k+":
age = 25
income = 50000
# Both conditions must be true
if age >= 18 and income >= 30000:
print("Approved")
# At least one must be true
if age >= 65 or income < 20000:
print("Discount eligible")
I've seen things like 0 < x < 10 in math. Can I do that in Python?
Yes — Python supports comparison chaining, and it works exactly like math notation:
x = 5
print(0 < x < 10) # True — x is between 0 and 10
print(0 < x < 3) # False — x is not less than 3
score = 85
print(80 <= score < 90) # True — score is in the B range
This is cleaner than writing x > 0 and x < 10. Most languages don't support this — it's one of Python's nice touches.
Is there an order of operations? Like, does and run before or?
Yes. The precedence is: not first, then and, then or. This matters:
# Without parentheses:
True or False and False # True (and runs first)
# With parentheses for clarity:
(True or False) and False # False
When in doubt, use parentheses. They make your intent clear and prevent bugs. This is exactly like the order of operations in your spreadsheet formulas — multiplication before addition — but with logic instead of math. Now let's write some boolean logic.
Practice your skills
Sign up to write and run code in this lesson.
Booleans & Logic
True, False, truthy/falsy values, logical operators, and comparison chaining.
OK so this is basically like if/else but going deeper, right? Last week we used True and False in conditions — if score >= 90: and stuff. But I feel like there's more going on.
There is. Last week you used booleans without really thinking about them. Every time you wrote if score >= 90:, Python was evaluating score >= 90 and getting back either True or False. This week, we're going to understand that machinery.
Booleans are Python's truth type. There are only two values: True and False. They're the result of every comparison, every condition check, every logical operation.
print(10 > 5) # True
print(10 == 5) # False
print(type(True)) # <class 'bool'>
Here's a fun fact: booleans are a subtype of integers. True is 1, False is 0. This means you can do math with them — which turns out to be surprisingly useful.
print(True + True) # 2
print(False + True) # 1
Wait, True is literally the number 1? That's wild. So if I had a list of pass/fail results, I could just... sum them?
Exactly. And that's a pattern you'll use in real data work. Imagine you have a spreadsheet column of scores and you want to count how many passed. In Excel you'd use COUNTIF. In Python, you can sum the booleans:
scores = [92, 55, 78, 43, 88]
passed = sum(score >= 60 for score in scores)
print(passed) # 3
Each score >= 60 produces True (1) or False (0), and sum() adds them up.
OK, and what about things that aren't True or False? I heard Python has "truthy" and "falsy" values.
Every value in Python has a boolean identity. When you put something in an if statement, Python converts it to True or False behind the scenes.
Falsy values — these all act like False:
bool(0) # False — zero
bool(0.0) # False — zero float
bool("") # False — empty string
bool([]) # False — empty list
bool(None) # False — None
Everything else is truthy:
bool(1) # True — any non-zero number
bool(-5) # True — even negative numbers
bool("hello") # True — non-empty string
bool([1, 2]) # True — non-empty list
This is why you can write if name: instead of if name != "":. Python treats the empty string as falsy. You'll see this pattern everywhere in real code.
How do and, or, and not work? I used and once last week but I was kind of guessing.
These are logical operators. They combine boolean values:
# and — True only if BOTH sides are True
print(True and True) # True
print(True and False) # False
# or — True if EITHER side is True
print(False or True) # True
print(False or False) # False
# not — flips the value
print(not True) # False
print(not False) # True
In practice, you combine comparisons. Think about the business rules you'd write in a spreadsheet — "approve if age is 18+ AND income is 30k+":
age = 25
income = 50000
# Both conditions must be true
if age >= 18 and income >= 30000:
print("Approved")
# At least one must be true
if age >= 65 or income < 20000:
print("Discount eligible")
I've seen things like 0 < x < 10 in math. Can I do that in Python?
Yes — Python supports comparison chaining, and it works exactly like math notation:
x = 5
print(0 < x < 10) # True — x is between 0 and 10
print(0 < x < 3) # False — x is not less than 3
score = 85
print(80 <= score < 90) # True — score is in the B range
This is cleaner than writing x > 0 and x < 10. Most languages don't support this — it's one of Python's nice touches.
Is there an order of operations? Like, does and run before or?
Yes. The precedence is: not first, then and, then or. This matters:
# Without parentheses:
True or False and False # True (and runs first)
# With parentheses for clarity:
(True or False) and False # False
When in doubt, use parentheses. They make your intent clear and prevent bugs. This is exactly like the order of operations in your spreadsheet formulas — multiplication before addition — but with logic instead of math. Now let's write some boolean logic.
Practice your skills
Sign up to write and run code in this lesson.