Lesson 8: Booleans and Comparisons


What you'll learn in this lesson


True or False: the boolean

A 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.


Comparisons ask yes-or-no questions

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.


Storing the answer

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.


The classic bug: = versus ==

This is the single most common beginner mistake in every programming language, so learn it now:

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.


Hands-on exercise

  1. In the shell, work out each of these before running them, then check: 5 > 3, 5 < 3, 5 == 5, 5 != 5, 4 >= 4, 4 <= 3.
  2. Store your age in a variable. Then store the answer to "am I 10 or older?" in a second variable and print it.
  3. Compare two names with == — once with the same name twice, once with different names — and explain both results.

Quick quiz

  1. What values can a boolean hold, and how are they spelled in Python?
  2. What does 10 != 9 ask, and what is the answer?
  3. What is the difference between = and ==?
  4. In old_enough = age >= 10, what is stored in old_enough, and why?
  5. Why does lowercase 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.


Key takeaways

Next lesson: If Statements.


Slides

Work through the slides with Helio's narration, or download them:

Open the slidesDownload PowerPointDownload PDF

Try it yourself

Run the lesson's example with real Python, right in your browser (Python loads from the internet, so you need to be online).

Open the demo

Practice file

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.

Check yourself

Five quick questions on this lesson. Take the Lesson 8 quiz

Want graded quizzes and progress tracking?

Enroll free in the full course — same 17 lessons, with your progress saved. Free, no payment.