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 8, 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 answersTrue 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.
Press play on any slide and the lesson plays on by itself from there. You can still click Next or click a slide to move on.
Download the Lesson 8 slides (PowerPoint, with narration)
Download the Lesson 8 slides (PDF)
5 > 3, 5 < 3, 5 == 5, 5 != 5, 4 >= 4, 4 <= 3.== — once with the same name twice,A comparison gives True or False. Here, the question is “is this age 10 or older?” Change the age and run again.
Try this: Change the age to 8 and press Run again — both answers become False. Try 15: the first stays True and the second becomes True.
Press Run to see the output here.
Save this as lesson08-practice.py and run it with F5 in IDLE.
# 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)
True or False, always capitalized.==, !=, >, <, >=, <=) answer yes-or-no questions.= stores; == asks. Mixing them up is the classicNext lesson: If Statements.
10 != 9 ask, and what is the answer?= and ==?old_enough = age >= 10, what is stored inold_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.