Booleans and Comparisons

Python for Absolute Beginners · Lesson 8

What you’ll learn

1 / 13

True or False: the boolean

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

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

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

9 / 13

Hands-on exercise

10 / 13

Key takeaways

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.