Skip to content
KodeTrail

Camp 2 · Step 8 of 23

Booleans and comparisons

True, False, and the comparison operators — the raw material every decision in every program is made of.

12 min+50 XP

Every decision a program makes — show this button? grant this discount? end this game? — boils down to a value that is either True or False. These are booleans (named after logician George Boole), and they're the third fundamental type on our trail.

Comparisons produce booleans

Python

The full toolkit:

OperatorMeaning
==equal to
!=not equal to
> / <greater / less than
>= / <=greater / less than or equal

Combining conditions: and, or, not

Python
  • and — True only if both sides are True
  • or — True if at least one side is True
  • not — flips the value

Comparisons and logic combine naturally:

age = 15
can_join = age >= 13 and age <= 19   # is this a teenager?
Checkpoint

What does this print?

steps = 8000
goal = 10000
print(steps >= goal)
Checkpoint

If a = True and b = False, what is (a or b) and (not b)?

What's next

One more piece and Camp 2 is yours: converting between types — turning "42" the text into 42 the number, and back.