Unit 2 · Programming 1 · Lesson 2.14

Logical Operators

One condition is fine, but real rules stack them: over a height and either with an adult or old enough. Today you join Booleans together with and, or, and not.

Big Idea 3 — Algorithms & ProgrammingCompound conditions

Before this lesson — read on Runestone

Covers and, or, and not and how they combine. 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, closing out Week 3’s teach days. You can build one Boolean (2.11) and act on it (2.12–2.13). Today you combine several Booleans into a single compound condition — the last piece before Decision Day.

2

The Hook

“You may ride if you are over 48″ AND (with an adult OR over 12).” That’s a real amusement-park sign, and it’s also real code. It has an and, an or, and parentheses that change the meaning — exactly the structure the exam tests.

Read it carefully: the height rule is non-negotiable (it’s joined by and), but the second half offers two ways to qualify (joined by or). Miss the parentheses and you’d let a 40-inch kid ride just for being over 12. Logic operators are how a program gets rules like this exactly right.

3

The Idea

Three logical operators combine Booleans into one:

OperatorPythonPseudocodeTrue when…
both must holdandANDboth sides are True
either will doorORat least one side is True
flip itnotNOTthe one side is False

The clearest way to nail these down is a truth table — every combination of inputs and the result:

ABA and BA or Bnot A
TrueTrueTrueTrueFalse
TrueFalseFalseTrueFalse
FalseTrueFalseTrueTrue
FalseFalseFalseFalseTrue
Two facts settle most exam questions: and is True only in that top row (both True); or is False only in that bottom row (both False). If you remember just the exceptions, you remember the whole table.

Python — the ride rule

height = 50
with_adult = False
age = 13
can_ride = height >= 48 and (with_adult or age > 12)
print(can_ride)   # True

AP Pseudocode

height ← 50
withAdult ← false
age ← 13
canRide ← height ≥ 48 AND (withAdult OR age > 12)
DISPLAY(canRide)

Hover or tap a line to light its twin. Trace line 4: 50 >= 48 is True; the parentheses give (False or True) = True; True and True = True. Drop the age > 12 to 10 and the whole thing flips to False — try it in the machine below.

A useful intuition (no formal name needed). “NOT (A AND B)” is not the same as “NOT A AND NOT B.” If it’s not true that you have both a ticket and an ID, that just means you’re missing at least one — which is “NOT A OR NOT B.” Flipping an and turns it into an or. You’ll meet this formally later; for now, just don’t assume “not” distributes without changing the connector.
4

Try It — The Ride Checker

Set the three inputs and watch each piece of the compound condition evaluate, then combine. This is the amusement-park sign as running code.

height = age =
Presets:
5

Vocabulary

logical operator Programs
An operator that combines or flips Boolean values: and, or, not (AND, OR, NOT in pseudocode).
compound condition Programs
A condition built from two or more Boolean expressions joined by logical operators.
truth table Programs
A table listing every combination of inputs and the resulting Boolean — the exact definition of an operator.
6

Check

Six questions. Evaluate the inside of the parentheses first, then combine — on paper — then pick your answer.

0 of 6 answered
7

Impact Check

The gates that filter everything

and, or, and not aren’t just code — they’re the logic behind every search box and filter you use. “Show me shoes that are size 10 and under $80 and (blue or black).” A spam filter, a database query, a content moderation rule — all of it is compound Boolean conditions deciding what you see and what you don’t.

Deep down, these same three operations are also how the physical chips in every device compute — logic gates wired from AND, OR, and NOT. You’re learning at the software level the exact idea the hardware is built from. It really is operators all the way down.

8

Connections

Came from 2.11–2.13 — you could build and act on one Boolean. Now you fuse several into a single condition an if can test.
Next in 2.15 — Decision Day. The Challenge tier needs compound conditions with input validation, straight from today.
Returns in 2.17 — Mini-Project 2 requires at least one compound condition, and you’ll write a paragraph explaining exactly how it works.
Returns on the exam — evaluating AND/OR/NOT expressions, especially with parentheses, is a guaranteed and very learnable point source.
← 2.13 elif and Nested Conditionals