True and False= and == are different, and why mixing them up is the classic beginner bugA boolean (named after the mathematician George Boole) is a value
that can only be one of two things: True or False. That is all.
No maybe, no in-between. Computers make millions of yes-or-no
decisions, and booleans are how those decisions are stored.
Note the capital letters: Python writes True and False with a
capital first letter. Lowercase true gives a NameError, because
Python only recognizes the capitalized forms.
print(True)
print(False)
Output:
True
False
On their own, True and False are not very interesting. They become
powerful when a comparison produces one of them — which is next.
A comparison compares two values and answers True or False.
Here are all six comparison operators:
| Operator | Meaning | Example | Result |
|---|---|---|---|
== |
Equal to | 10 == 10 |
True |
!= |
Not equal to | 10 != 9 |
True |
> |
Greater than | 10 > 9 |
True |
< |
Less than | 10 < 9 |
False |
>= |
Greater than or equal to | 10 >= 10 |
True |
<= |
Less than or equal to | 10 <= 9 |
False |
Try them:
print(10 > 9)
print(10 < 9)
print(10 == 10)
print(10 != 9)
print(7 >= 7)
print(7 <= 6)
Output:
True
False
True
True
True
False
Comparisons work on strings too, in dictionary (alphabetical) order:
print("apple" < "banana")
print("Sam" == "Sam")
Output:
True
True
"apple" < "banana" is True because "apple" comes before "banana"
alphabetically. You will rarely compare strings this way, but it is
good to know the same operators work on both numbers and text.
A comparison's answer is just a value, so it can live in a variable like anything else:
age = 12
old_enough = age >= 10
print(old_enough)
Output:
True
old_enough now holds True. Later, in Lesson 9, you will use
variables like this to make decisions: "if old_enough, do this."
Storing the answer separately keeps the decision readable — the
variable's name says what the question meant.
= versus ==This is the single most common beginner mistake in every programming language, so learn it now:
= stores a value: age = 12 puts 12 into age.== asks whether two values are equal: age == 12 answers
True or False.Using one = where you meant two does not ask a question — it stores
a value, usually by accident. If a comparison ever seems to "always be
true" or your program behaves oddly around a decision, check whether
you wrote = instead of == first.
5 > 3, 5 < 3, 5 == 5, 5 != 5, 4 >= 4, 4 <= 3.== — once with the same name twice, once
with different names — and explain both results.10 != 9 ask, and what is the answer?= and ==?old_enough = age >= 10, what is stored in old_enough, and why?true give an error?Answer key: 1. Only True or False, always capitalized. 2. "Is 10 not equal to 9?" — True. 3. = stores a value; == asks whether two values are equal. 4. A boolean — the answer to the comparison age >= 10. 5. Python only recognizes the capitalized forms True and False.
True or False, always capitalized.==, !=, >, <, >=, <=) answer yes-or-no questions.= stores; == asks. Mixing them up is the classic beginner bug.Next lesson: If Statements.
Work through the slides with Helio's narration, or download them:
Open the slidesDownload PowerPointDownload PDF
Run the lesson's example with real Python, right in your browser (Python loads from the internet, so you need to be online).
Download lesson08-practice.pyDownload the solution
# Lesson 8: Booleans and Comparisons
# WHAT TO DO: A boolean is an answer that is either True or False.
# This program stores an age of 12 and should answer "am I 10 or older?"
# But one line is wrong. Fix line 10: change < to >= in the comparison.
# HINT: 12 is 10 or older, so the answer should be True. Only change
# the comparison sign; do not change the age or the number 10.
# Run with F5 in IDLE. You are done when the last line prints True.
age = 12
old_enough = age < 10 # <-- CHANGE THIS LINE: should ask "10 or older"
print(age)
print(old_enough)
Save the file as lesson08-practice.py and run it with F5 in IDLE. On a Mac, if F5 does nothing, use Run, then Run Module, from the menu.
Five quick questions on this lesson. Take the Lesson 8 quiz
Enroll free in the full course — same 17 lessons, with your progress saved. Free, no payment.