Booleans and None
True, False, and ‘nothing’
In this lesson
A boolean is a value that is either True or False — how Python represents a yes/no decision. You’ll learn how comparisons produce booleans, which values count as ‘truthy’, and what the special value None means.
Explain it like I’m 5
A boolean is a yes/no answer. None is different — it’s Python’s way of saying ‘there’s nothing here yet’, which is not the same as ‘no’.
Booleans come from comparisons
Comparisons produce a boolean: 3 > 1 is True, 2 == 5 is False. The comparison operators are == (equal), != (not equal), and < > <= >=. These are the conditions you’ll feed to if statements next unit.
print(3 > 1)
print(2 == 5)
print(7 != 4)
True False True
Truthiness
name = ""
if name:
print("Got a name")
else:
print("Name is empty")
Name is empty
Combining conditions: and, or, not
and is True only if both sides are; or is True if either side is; not flips a boolean. Example: age >= 13 and age <= 19 checks the teen range. Use parentheses when mixing them, for clarity.
age = 16
is_teen = age >= 13 and age <= 19
print(is_teen)
print(not is_teen)
True False
Set a score, then print whether it's a passing score (50 or above) AND not a perfect 100.
score = 72
print(score >= 50 and score != 100)
Combine two comparisons with and.
score = 72
passed_not_perfect = score >= 50 and score != 100
print(passed_not_perfect)
True
None: the absence of a value
None represents ‘no value’. A function that doesn’t explicitly return anything returns None. Crucially, None is not False, not 0, and not "" — it’s its own thing. Test for it with is: if result is None:.
Short-circuiting and chained comparisons
Two conveniences make boolean logic both faster and more readable. First, and and or short-circuit: in a and b, if a is already false, Python never bothers checking b — the answer can’t change. This lets you guard a risky test, e.g. count > 0 and total / count > 5 safely avoids dividing when count is zero.
Second, you can chain comparisons the way maths does: 0 <= score <= 100 is true only when score sits in that range — no and required.
age = 20
print(0 <= age <= 17)
print(age >= 18 and age < 65)
user = ""
print(user or "guest")
False True guest
Common mistake: Treating None like False or 0
They’re all falsy, so they behave alike in an if, but they are different values. None == 0 is False.
When you specifically mean ‘not set’, test with is None. Reserve 0 and False for actual numbers and booleans.
Which value is truthy?
A non-empty string is truthy; 0 and the empty string are falsy.
What does (5 > 3) or (1 > 9) evaluate to?
or is True if either side is True; the first comparison is True.
Mini exercise (easy)
Given a variable stock, print True if it is greater than 0, otherwise False — using a single comparison.
Do it here. Finish the code below and press Run to try your answer right in the browser — no setup needed.
stock = 5
in_stock = False # TODO: True if stock is greater than 0
print(in_stock)
print(stock > 0) already produces a boolean.
stock = 5
in_stock = stock > 0
print(in_stock)
True
__no_output_bypass__ = True
import ast as _ast
assert any(isinstance(n, _ast.Compare) for n in _ast.walk(_ast.parse(__user_code__))), "use a comparison like stock > 0 — don't hardcode True"
__no_output_bypass__ = False
assert in_stock is True, "in_stock should be True when stock is above 0"
print("✓ Correct!")