Booleans and Comparisons

 

What you’ll learn in this lesson

  • What a boolean is: True and False
  • How comparisons ask yes-or-no questions
  • All six comparison operators
  • How to store a comparison’s answer in a variable
  • Why = and == are different, and why mixing them up is the classic beginner bug

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


The classic bug: = 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.

Booleans and Comparisons

Python for Absolute Beginners · Lesson 8

What you’ll learn

  • What a boolean is: True and False
  • How comparisons ask yes-or-no questions
  • All six comparison operators
  • How to store a comparison's answer in a variable
  • Why = and == are different, and why mixing them up is the classic beginner bug
1 / 13

True or False: the boolean

  • A boolean is a value that can only be one of two things: True or False — no maybe, no in-between
  • Named after the mathematician George Boole
  • Note the capital letters: Python writes True and False with a capital first letter
  • Lowercase true gives a NameError — Python only recognizes the capitalized forms
2 / 13

Booleans in code

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.

3 / 13

Comparisons ask yes-or-no questions

  • A comparison compares two values and answers True or False
  • == equal to · != not equal to · > greater than · < less than
  • >= greater than or equal to · <= less than or equal to
  • Comparisons work on strings too, in dictionary (alphabetical) order
4 / 13

Comparisons in code

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

Each line asks a yes-or-no question, and Python answers True or False.

5 / 13

Comparing strings in code

print("apple" < "banana")
print("Sam" == "Sam")

Output:
True
True

“apple” comes before “banana” alphabetically, so the first answer is True.

6 / 13

Storing the answer

  • A comparison's answer is just a value, so it can live in a variable like anything else
  • Storing the answer separately keeps the decision readable — the variable's name says what the question meant
  • In Lesson 9, you will use variables like this to make decisions
7 / 13

Storing the answer in code

age = 12
old_enough = age >= 10
print(old_enough)

Output:
True

old_enough now holds True — the answer to “is age 10 or older?”

8 / 13

The classic bug: = versus ==

  • = 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,” check whether you wrote = instead of == first
9 / 13

Hands-on exercise

  • 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
  • Store your age in a variable, then store the answer to “am I 10 or older?” in a second variable and print it
  • Compare two names with == — once with the same name twice, once with different names — and explain both results
10 / 13

Key takeaways

  • A boolean is True or False, always capitalized
  • Comparisons (==, !=, >, <, >=, <=) answer yes-or-no questions
  • A comparison's answer can be stored in a variable for later use
  • = stores; == asks. Mixing them up is the classic beginner bug
11 / 13

Take the quiz

Take the Lesson 8 quiz — 5 quick questions to check what you learned.

12 / 13

Next lesson

If Statements: Making Decisions

13 / 13

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)


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.

Lesson 08: Booleans and Comparisons

A comparison gives True or False. Here, the question is “is this age 10 or older?” Change the age and run again.

Loading Python… this can take a few seconds the first time. Please wait — the Run button will become available when Python is ready.

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.

The code that runs



Output

Press Run to see the output here.

Practice file

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)

Key takeaways

  • A boolean is True or False, always capitalized.
  • Comparisons (==, !=, >, <, >=, <=) answer yes-or-no questions.
  • A comparison's answer can be stored in a variable for later use.
  • = stores; == asks. Mixing them up is the classic
    beginner bug.

Next lesson: If Statements.


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.