3.05 Boolean Expressions
Relational and logical operators that evaluate to True or False.
- Reference Guide
- College Board Requirements
- Lesson Plan
- Code Examples
- A. Simple: Comparisons Create Booleans
- Code Runner Challenge
- Popcorn Hack 1: Check One SFI Part Record
- Code Runner Challenge
- B. Combine Conditions
- Code Runner Challenge
- Popcorn Hack 2: Backend Create Rule
- Code Runner Challenge
- C. Compound Validation Rule
- Code Runner Challenge
- D. Use a Boolean to Control a Decision
- Code Runner Challenge
- Popcorn Hack 3: SFI Search Filter
- In-class MCQ Check
- Independent Homework
- References
Reference Guide
Key Vocabulary
| Term | Definition | Example |
|---|---|---|
| Boolean | A value that is either True or False. |
is_valid = True |
| Boolean expression | An expression that evaluates to True or False. |
count > 0 |
| Relational operator | Compares two values. | ==, !=, >, <, >=, <= |
| Logical operator | Combines or reverses Boolean values. | and, or, not |
| Compound expression | A Boolean expression with multiple conditions. | has_name and not duplicate |
| Validation | Checking whether data satisfies required rules. | approve/reject an SFI record |
Truth Guide
| A | B | A and B |
A or B |
|---|---|---|---|
| True | True | True | True |
| True | False | False | True |
| False | True | False | True |
| False | False | False | False |
not True is False, and not False is True.
Translating English Rules
- All requirements must pass → use
and. - Either option is acceptable → use
or. - Reject a duplicate → often use
not duplicate. - Break a long rule into named Boolean variables before combining it.
- Test valid, invalid, and edge cases instead of checking only one record.
College Board Requirements
AP CSP Topic 3.5: Boolean Expressions focuses on writing and evaluating relational expressions. The course framework identifies Boolean values as true/false and uses relational operators to compare two variables, expressions, or values.
College Board Pseudocode
AP CSP questions use College Board pseudocode rather than Python syntax. Be able to recognize the same Boolean logic in both forms.
Relational and Logical Operators
| College Board pseudocode | Python |
|---|---|
a = b |
a == b |
a ≠ b |
a != b |
a > b |
a > b |
a < b |
a < b |
a ≥ b |
a >= b |
a ≤ b |
a <= b |
NOT condition |
not condition |
condition1 AND condition2 |
condition1 and condition2 |
condition1 OR condition2 |
condition1 or condition2 |
SFI Validation in College Board Pseudocode
Tech Talk 1 - Compare SFI Part Data
spec_number = "1.1"
category = "Auto Racing"
part_count = 12
print("Spec is 1.1:", spec_number == "1.1")
print("Category is not Street Car:", category != "Street Car")
print("At least one part record exists:", part_count >= 1)
Code Runner Challenge
Tech Talk 1 - Compare SFI Part Data
View IPYNB Source
hasProductName ← (productName ≠ "")
validCategory ← (category = "Auto Racing") OR (category = "Drag Racing")
hasSpecNumber ← (specNumber ≠ "")
hasEffectiveDate ← (effectiveDate ≠ "")
duplicateSpec ← (specNumber = "1.1") OR (specNumber = "2.1")
isValid ← hasProductName AND validCategory AND hasSpecNumber
AND hasEffectiveDate AND NOT duplicateSpec
IF (isValid)
{
DISPLAY("ACCEPT RECORD")
}
ELSE
{
DISPLAY("REJECT RECORD")
}
Read It Like the Exam
- Evaluate each relational expression first.
- Apply
NOTto the condition it belongs to. - Combine conditions with
AND/OR. - Determine whether the
IFcondition is true or false. - Be careful: College Board comparison uses
=, while Python comparison uses==.
Pseudocode Check
For one of your own test records, predict the pseudocode output before running the Python validator. Then verify that both representations make the same decision.
Lesson Plan
Learning Objective: Write and evaluate Boolean expressions that compare values and combine multiple backend requirements.
Success Criteria: You can predict whether an expression is True or False, explain why, choose the correct relational/logical operator, and test a compound rule with more than one input.
Lesson Theme — SFI Safety Tech Inspector
Theme: You are building validation logic for an SFI automotive safety-spec backend.
Every example uses the same story instead of switching contexts:
- Product: an automotive safety component such as a flywheel or clutch assembly.
- Category: a racing category such as Auto Racing or Drag Racing.
- Spec number: the identifier that must be present and unique.
- Effective date: required metadata for an approved record.
- Backend decision: accept, reject, return, or filter a record.
The theme matters because Boolean expressions become easier to reason about when every condition answers a real backend question:
“Does this record satisfy every rule required before it is accepted?”
As the lesson progresses, students move from checking one field to validating an entire candidate record.
Teaching rhythm
Pause for each Popcorn Hack during the lesson: give students about two minutes to predict, edit and run the nearby code, then ask a partner to explain the result before continuing. Students copy their work and visible output into their own notebook after the live check. Save the independent Homework Hack for after class.
Tech Talk (short segments between checks)
A Boolean expression is a question whose result is only True or False.
Python comparison operators:
| Python | Meaning | SFI example |
|---|---|---|
== |
equal to | spec_number == "1.1" |
!= |
not equal to | category != "Street Car" |
> |
greater than | part_count > 0 |
< |
less than | part_count < 100 |
>= |
greater than or equal to | part_count >= 1 |
<= |
less than or equal to | part_count <= 100 |
Logical operators combine Boolean values:
| Operator | Meaning | Example |
|---|---|---|
and |
both conditions must be true | name exists and spec exists |
or |
at least one condition is true | Auto Racing or Drag Racing |
not |
reverses a Boolean value | not a duplicate |
Remember: = assigns a value in Python, while == compares values.
Code Examples
A. Simple: Comparisons Create Booleans
Popcorn Hack 1 - Check SFI Part Fields
part = {
"product_name": "Replacement Flywheels and Clutch Assemblies",
"category": "Auto Racing",
"spec_number": "1.1",
"effective_date": "Nov. 9, 2001"
}
valid_categories = ["Auto Racing", "Drag Racing"]
# Build and test your solution below.
Code Runner Challenge
Popcorn Hack 1 - Check SFI Part Fields
View IPYNB Source
# CODE_RUNNER: Tech Talk 1 - Compare SFI Part Data
spec_number = "1.1"
category = "Auto Racing"
part_count = 12
print("Spec is 1.1:", spec_number == "1.1")
print("Category is not Street Car:", category != "Street Car")
print("At least one part record exists:", part_count >= 1)
Popcorn Hack 1: Check One SFI Part Record
An SFI automotive record is complete when it has a product name, a racing category, a spec number, and an effective date.
For this challenge, Auto Racing and Drag Racing are valid racing categories. Your finished runner should report the Boolean state of each requirement for the record below.
In class (2 minutes): Predict the first output, complete the runner, run it, then change one input and compare with a partner. Save your code and output in your personal notebook.
Tech Talk 2 - Combine SFI Backend Checks
has_product_name = True
has_spec_number = True
duplicate_spec = False
print("Required fields present:", has_product_name and has_spec_number)
print("At least one field present:", has_product_name or has_spec_number)
print("Spec is new:", not duplicate_spec)
Code Runner Challenge
Tech Talk 2 - Combine SFI Backend Checks
View IPYNB Source
# CODE_RUNNER: Popcorn Hack 1 - Check SFI Part Fields
part = {
"product_name": "Replacement Flywheels and Clutch Assemblies",
"category": "Auto Racing",
"spec_number": "1.1",
"effective_date": "Nov. 9, 2001"
}
valid_categories = ["Auto Racing", "Drag Racing"]
# Build and test your solution below.
B. Combine Conditions
Backend validation usually needs several checks at once.
Popcorn Hack 2 - Backend Create Rule
part = {
"product_name": "Replacement Flywheels and Clutch Assemblies",
"category": "Auto Racing",
"spec_number": "1.1",
"effective_date": "Nov. 9, 2001"
}
valid_categories = ["Auto Racing", "Drag Racing"]
existing_spec_numbers = ["1.1", "2.1"]
# Build and test your solution below.
Code Runner Challenge
Popcorn Hack 2 - Backend Create Rule
View IPYNB Source
# CODE_RUNNER: Tech Talk 2 - Combine SFI Backend Checks
has_product_name = True
has_spec_number = True
duplicate_spec = False
print("Required fields present:", has_product_name and has_spec_number)
print("At least one field present:", has_product_name or has_spec_number)
print("Spec is new:", not duplicate_spec)
Popcorn Hack 2: Backend Create Rule
The backend receives the same style of SFI part record. A new database record should only be created when all required fields are valid and its spec number is not already stored.
Build the final create decision and test it with at least one valid case and one rejected case.
In class (2 minutes): Decide what the first run should show, complete and run the code, then change one input to test the opposite case. Explain the result aloud and transfer both cases to your notebook.
Tech Talk 3 - SFI Record Validation
has_product_name = True
has_spec_number = True
valid_category = True
duplicate_spec = False
basic_record_is_valid = (
has_product_name
and has_spec_number
and valid_category
and not duplicate_spec
)
print("Basic SFI record is valid:", basic_record_is_valid)
Code Runner Challenge
Tech Talk 3 - SFI Record Validation
View IPYNB Source
# CODE_RUNNER: Popcorn Hack 2 - Backend Create Rule
part = {
"product_name": "Replacement Flywheels and Clutch Assemblies",
"category": "Auto Racing",
"spec_number": "1.1",
"effective_date": "Nov. 9, 2001"
}
valid_categories = ["Auto Racing", "Drag Racing"]
existing_spec_numbers = ["1.1", "2.1"]
# Build and test your solution below.
C. Compound Validation Rule
Build small Boolean checks first, then combine them into one readable result.
Tech Talk 4 - SFI Backend Decision
part_in_database = True
record_archived = False
can_return_record = part_in_database and not record_archived
if can_return_record:
print("Return the SFI part record")
else:
print("Do not return the SFI part record")
Code Runner Challenge
Tech Talk 4 - SFI Backend Decision
View IPYNB Source
# CODE_RUNNER: Tech Talk 3 - SFI Record Validation
has_product_name = True
has_spec_number = True
valid_category = True
duplicate_spec = False
basic_record_is_valid = (
has_product_name
and has_spec_number
and valid_category
and not duplicate_spec
)
print("Basic SFI record is valid:", basic_record_is_valid)
D. Use a Boolean to Control a Decision
An if statement can use the final Boolean result to decide whether the backend accepts, returns, or rejects a record.
Popcorn Hack 3 - SFI Search Filter
query = "1.2"
records = [
{
"product_name": "Replacement Flywheels and Clutch Assemblies",
"category": "Auto Racing",
"spec_number": "1.1"
},
{
"product_name": "Multiple Disc Clutch Assemblies",
"category": "Drag Racing",
"spec_number": "1.2"
},
{
"product_name": "Racing Flywheel Record",
"category": "Auto Racing",
"spec_number": "2.1"
}
]
valid_categories = ["Auto Racing", "Drag Racing"]
# Build and test your solution below.
Code Runner Challenge
Popcorn Hack 3 - SFI Search Filter
View IPYNB Source
# CODE_RUNNER: Tech Talk 4 - SFI Backend Decision
part_in_database = True
record_archived = False
can_return_record = part_in_database and not record_archived
if can_return_record:
print("Return the SFI part record")
else:
print("Do not return the SFI part record")
Popcorn Hack 3: SFI Search Filter
An SFI backend search can match a record through either its product name or its exact spec number. Search results should only include records from accepted racing categories.
Use the provided records and query to return only valid matches. Your logic should still work when the query or record data changes.
In class (3 minutes): Predict a match and a nonmatch, complete and run the code, then change the query or action. Share why the result changed; preserve your tested work in your notebook.
# CODE_RUNNER: Popcorn Hack 3 - SFI Search Filter
query = "1.2"
records = [
{
"product_name": "Replacement Flywheels and Clutch Assemblies",
"category": "Auto Racing",
"spec_number": "1.1"
},
{
"product_name": "Multiple Disc Clutch Assemblies",
"category": "Drag Racing",
"spec_number": "1.2"
},
{
"product_name": "Racing Flywheel Record",
"category": "Auto Racing",
"spec_number": "2.1"
}
]
valid_categories = ["Auto Racing", "Drag Racing"]
# Build and test your solution below.
In-class MCQ Check
4 questions, one at a time. Answer each question, check it, then continue.
Independent Homework
The three Popcorn Hacks above are live understanding checks. You are responsible for copying or preparing their code, predictions, and visible output in your personal notebook; the lesson page does not collect that work for you. Do the Homework Hack independently after the lesson.
Homework Hack (independent): SFI Backend Validator
Build a reusable validator for candidate SFI automotive records.
A valid record has:
- a non-empty product name,
- an accepted racing category,
- a non-empty spec number,
- an effective date,
- and a spec number that does not already exist in the backend.
The provided test data includes different cases. Your completed runner should clearly print whether each candidate record is accepted or rejected.
Copy this starter into your personal notebook after class. Complete it there, run multiple tests, and include the output. This is a reference block, not an in-page runner:
# Homework Hack - SFI Backend Validator
existing_spec_numbers = ["1.1", "2.1"]
valid_categories = ["Auto Racing", "Drag Racing"]
test_records = [
{
"product_name": "Multiple Disc Clutch Assemblies",
"category": "Auto Racing",
"spec_number": "1.2",
"effective_date": "Feb. 9, 2006"
},
{
"product_name": "Replacement Flywheels",
"category": "Street Car",
"spec_number": "3.1",
"effective_date": "Jan. 1, 2026"
},
{
"product_name": "Existing Flywheel Record",
"category": "Drag Racing",
"spec_number": "1.1",
"effective_date": "Jan. 1, 2026"
}
]
# Build and test your validator below.
References
College Board. AP Computer Science Principles Course and Exam Description, Topic 3.5 Boolean Expressions and the AP CSP Exam Reference Sheet.
Python Software Foundation. Python Documentation: Boolean Operations and Comparisons.