Unit 2 · Programming 1 · Lesson 2.11

Booleans and Comparisons

Every program that makes a choice is secretly asking yes/no questions. Today you meet the two answers a computer can give — and the one-character typo that breaks more beginner programs than anything else.

Big Idea 3 — Algorithms & ProgrammingWeek 3 · Decisions start here

Before this lesson — read on Runestone

Covers Boolean values and comparison operators — the raw material for everything in Week 3. Enrolled in our Runestone course? Open it from there so your progress counts.

1

Where We Are

People→ Bits→ Programs→ Internet→ Security→ Big Data→ Impact

Still in Programs, opening Week 3. For two weeks your programs ran the same way every time. This week they start making decisions — and a decision needs a yes/no question to decide on. That question is today.

2

The Hook

face_matches == True Every app on your phone is a pile of yes/no questions running thousands of times a second. Unlock the screen: does the face match? Send a text: is there a signal? Open a game: is the player alive?

Each question has exactly one of two answers — True or False. Those two words are a whole data type, named after George Boole, and once you can produce them on purpose you can make a program choose.

3

The Idea

A Boolean value is one of exactly two things: True or False (capital first letter in Python, always). A Boolean expression is any expression that evaluates to one of those two — usually by comparing two values with a comparison operator.

QuestionPythonAP PseudocodeExample
equal to?===7 == 7 → True
not equal to?!=≠5 != 5 → False
less than?<<3 < 8 → True
greater than?>>3 > 8 → False
less than or equal?<=≤3 <= 3 → True
greater than or equal?>=≥6 >= 6 → True
The one-character bug that will bite you this week. = means assign (“put this value in the box”). == means compare (“are these two equal?”). Writing if x = 5 when you meant if x == 5 is the single most common mistake in Week 3. When you compare, you need two equals signs.

Python

age = 16          # one =  : assignment
can_drive = age >= 16   # a Boolean
print(can_drive)      # True
print(age == 18)      # two == : compare

AP Pseudocode

age ← 16
canDrive ← age ≥ 16
DISPLAY(canDrive)
DISPLAY(age = 18)

Hover or tap a line to light its twin. Notice the flip: pseudocode uses a single = for the comparison that Python writes as ==, and a left arrow ← for the assignment Python writes as =. Read both; write Python.

Comparisons work on text too, and text is case-sensitive: "cat" == "Cat" is False, because a capital C is a different character (a different bit pattern — remember Unit 1) than a lowercase c.
MOD comes back already. The most common Boolean expression in all of programming is n % 2 == 0 — “is n even?” It reads right to left: first compute n % 2 (the remainder), then compare that to 0. You met MOD in 2.3; here it becomes a yes/no question.
4

Try It — The Comparison Machine

Pick two numbers and an operator. The machine shows you the Boolean expression and the single value it evaluates to. Predict the answer before you read it.

a = b =
Presets:
expression: —
evaluates to: —
5

Vocabulary

Boolean value Programs
One of exactly two values: True or False. Named after mathematician George Boole.
Boolean expression Programs
An expression that evaluates to True or False — usually a comparison like age >= 16.
comparison (relational) operator Programs
An operator that compares two values and produces a Boolean: ==, !=, <, >, <=, >=.
6

Check

Six questions. Evaluate each expression on paper first — that’s the exam skill. Pick an answer for instant feedback.

0 of 6 answered
7

Impact Check

Two answers, and everything a computer decides

It is a genuinely strange fact that all of computing’s decisions — a self-driving car braking, a bank flagging a charge, a spam filter, a medical alert — bottom out in expressions that are only ever True or False. There is no “maybe” at this level. Complexity comes from combining thousands of these tiny yes/no answers, not from any single one being clever.

That is also where bias can hide. A loan program that decides with income >= threshold looks neutral, but someone chose the threshold. The Boolean is honest about being a rule; whether the rule is fair is a human question the code can’t answer. You’ll come back to this in Unit 8.

8

Connections

Came from 2.3 — n % 2 was just a number. Compare it to 0 and it becomes the yes/no question “is n even?”
Next in 2.12 — a Boolean on its own does nothing. Feed it to an if and the program finally acts on the answer.
Returns in 2.14 — you’ll join Booleans together with and, or, and not to ask compound questions.
Returns on the exam — evaluating a Boolean expression, and spotting = vs ==, are guaranteed multiple-choice points.
← 2.10 Lab: Mad Libs