Boolean logic
Master comparison and logical operators, truthy/falsy values, and short-circuit evaluation.
- Use comparison operators to produce True or False
- Combine conditions with and, or, not
- Explain truthy and falsy values in Python
- Apply short-circuit evaluation and chained comparisons
Every if and while statement relies on a condition that is either True or
False. Understanding how Python builds and evaluates those conditions gives you
much finer control over your programs' behaviour.
Comparison operators
Six operators compare two values and always return a boolean:
x = 7
x == 7 # True — equal to
x != 5 # True — not equal to
x > 3 # True — greater than
x < 10 # True — less than
x >= 7 # True — greater than or equal to
x <= 6 # False — less than or equal toA common mistake is writing = (assignment) where == (comparison) was meant.
Python will raise a SyntaxError in most of those cases, but it's still worth
internalising the distinction early.
Logical operators
Three keywords combine boolean values:
| Expression | Meaning |
|---|---|
a and b | True only if both are true |
a or b | True if at least one is true |
not a | Flips True to False and vice versa |
age = 20
has_ticket = True
age >= 18 and has_ticket # True — both conditions met
age < 18 or has_ticket # True — one is enough
not has_ticket # False — flippedTruthy and falsy values
Python's if statement doesn't require a literal True or False — it accepts
any expression and evaluates it as "truthful" or "falsy". These values are always
falsy:
FalseandNone- Zero:
0,0.0 - Empty containers:
"",[],{},set()
Everything else is truthy. This lets you write natural-sounding guards:
name = input_value # could be an empty string
if name:
print(f"Hello, {name}")
else:
print("No name provided")That if name: is more idiomatic than if name != "": and says the same thing.
Short-circuit evaluation
Python evaluates and and or lazily — this is short-circuit evaluation: it stops as soon as the result is
determined:
a and b: ifais falsy, Python never looks atb(the result is alreadyFalse).a or b: ifais truthy, Python never looks atb(the result is alreadyTrue).
This is useful as a guard pattern:
text = ""
length = text and len(text) # text is falsy → short-circuits to ""
# avoids calling len() on None, etc.And for providing defaults:
display_name = user_name or "Anonymous"Short-circuit evaluation isn't just a performance detail — it's a safety
feature. The pattern obj and obj.method() avoids calling a method on None
because the right side is never evaluated when obj is falsy.
Chained comparisons
Python lets you chain comparisons the way mathematicians write them, which reads
far more naturally than the equivalent and:
x = 7
1 < x < 10 # True — x is between 1 and 10 (exclusive)
0 <= x <= 100 # True — x is in the 0–100 range
# Equivalent but more verbose:
1 < x and x < 10Each chained comparison evaluates each value once and short-circuits, so
1 < x < 10 is both readable and efficient.
Write is_teen(age) that returns True if age is between 13 and 19 inclusive, False otherwise. Use a chained comparison.
is_teen(16) → Trueis_teen(20) → FalseDon't confuse is with ==. is tests object identity (same object in
memory); == tests value equality. Use == for almost everything. The
idiomatic exception: x is None (not x == None) because None is a
singleton and identity is exactly what you want to check.
Where to go next
You can now write precise conditions for any branch or loop. Next: reading and writing files — applying conditions when processing real data from disk.