Part 1 — Fundamentals · Estimated time: 20–30 min · Prerequisite: 02 · Data Types & Numbers
A boolean is a value that is either True or False. Booleans are how
programs answer yes/no questions — and answering questions is how programs make
decisions (which you'll do for real in the next lesson, Conditionals). Here we
learn how to produce True/False values by comparing and combining things.
- The two boolean values:
TrueandFalse - Comparison operators:
==,!=,<,>,<=,>= - Logical operators:
and,or,not - "Truthiness" — which everyday values count as
TrueorFalse - The classic
=vs==mix-up
is_open = True
is_raining = False
print(type(is_open)) # <class 'bool'>Comparisons produce booleans — that's where they usually come from:
print(10 > 3) # True
print(2 == 5) # Falsepython examples/01_true_and_false.py
| Operator | Means | Example | Result |
|---|---|---|---|
== |
equal to | 5 == 5 |
True |
!= |
not equal to | 5 != 3 |
True |
> |
greater than | 5 > 8 |
False |
< |
less than | 5 < 8 |
True |
>= |
greater or equal | 5 >= 5 |
True |
<= |
less or equal | 4 <= 3 |
False |
⚠️ =vs==: a single=assigns a value (x = 5). A double==compares (x == 5asks "is x equal to 5?"). Mixing them up is the #1 beginner bug.
python examples/02_comparison_operators.py
Combine yes/no answers into a bigger answer.
print(True and False) # False -> and needs BOTH to be True
print(True or False) # True -> or needs only ONE to be True
print(not True) # False -> not flips it
age = 25
print(age >= 18 and age < 65) # True -> "an adult under 65"python examples/03_logical_operators.py
💡 Try it yourself: write a comparison that is
Trueonly when a numbernis outside the range 0–100 (less than 0 or more than 100).
Python can treat almost any value as a yes/no answer. These count as falsy
(act like False): 0, 0.0, "" (empty string), [] (empty list), and
None. Everything else is truthy. bool(value) shows you which.
print(bool(0)) # False
print(bool("")) # False
print(bool("hi")) # True
print(bool([])) # False
print(bool([1, 2])) # TrueThis is why you'll later see if name: meaning "if name isn't empty."
python examples/04_truthiness.py
| Mistake | What happens | Fix |
|---|---|---|
if x = 5: |
SyntaxError |
Use == to compare: x == 5. |
True vs "True" |
the string "True" is just text (and always truthy) |
Use the real keyword True, no quotes. |
1 < x < 10 confusion |
actually this works in Python! | Chained comparisons are allowed and read naturally. |
not x == y ambiguity |
works, but clearer as x != y |
Prefer != for "not equal". |
True, False # the two boolean values
a == b a != b # equal / not equal
a < b a > b # less / greater
a <= b a >= b # less-or-equal / greater-or-equal
a and b # both must be True
a or b # at least one True
not a # flip it
bool(value) # truthy or falsy?
# falsy: 0, 0.0, "", [], None (everything else is truthy)Run a file with python exercises/<file>.py, then compare with the matching
file in solutions/.
exercises/01_can_vote.py— turn an age into a True/False "can vote?" answer.exercises/02_in_range.py— check whether a number sits inside a range.exercises/03_truthy_or_falsy.py— predict and print the truthiness of several values.
Try each yourself before opening the solution.
Next → 05 · Operators, Expressions & Statements (coming soon)