All lessons

Strings & booleans · step 7 of 22

Booleans & comparisons

7 minTrue, False, and how Python decides which is which.

Only two values

A boolean is either True or False — capital first letter, no quotes. Booleans usually come from a comparison.

print(10 > 7)      # True
print(10 == 7)     # False — == asks "are these equal?"
print(10 != 7)     # True
print(5 >= 5)      # True
print("a" == "A")  # False — comparisons are case sensitive

= stores a value; == compares two. Mixing them up is the most common beginner bug.

Truthiness

Some values count as false even though they are not False: 0, "" (empty text), [] (an empty list) and None. Everything else is truthy. bool(0) is False; bool("hi") is True. Booleans are exactly what if statements and loops use to make decisions — that is the next unit.

Remember this

  • Booleans are True and False
  • == compares while = stores
  • 0, '' and [] count as false

Quick quiz

Three questions. 3 correct answers and you have the idea — you can retry as often as you like.

  1. 1. What does print(4 == 4) show?

  2. 2. Which sign asks 'are these two equal?'

  3. 3. What is bool(0)?

Answer all three to check them.

Now write it yourself

0/2 passed
1

Practice 1

Print whether 10 is greater than 7.

practice_1.py
2

Practice 2

`age` is set for you. Print whether this person is old enough to vote (18 or older) — your program should work for any age.

practice_2.py

Given for you

age = 17

Finish with two of three quiz answers correct and every exercise passing, and this lesson is marked complete.