Comparison & Boolean Operators
Build conditions that evaluate to True or False.
Comparison operators compare values. Boolean operators combine or reverse conditions. Together they are used by selection and iteration.
Predict
Read the code first. Do not run it yet.
age = 15
print(age >= 13 and age <= 19)
print(age == 16)
print(not age < 13)- Predict the three Boolean outputs.
- Which expression uses AND?
- What does
notdo to a Boolean result?
Investigate
Use this code to explain exactly how the program works.
score = 72
submitted = True
print(score >= 70 and submitted == True)
print(score < 40 or submitted == False)- Break each line into its individual comparisons.
- Evaluate each comparison first, then combine the results.
- Why is
==used instead of=?
Comparison operators
| Meaning | General form | Python |
|---|---|---|
| Equal to | == | == |
| Not equal to | != | != |
| Less than | < | < |
| Less than or equal | <= | <= |
| Greater than | > | > |
| Greater than or equal | >= | >= |
Boolean operators
| General form | Python | Meaning |
|---|---|---|
| AND | and | True only when both conditions are True |
| OR | or | True when at least one condition is True |
| NOT | not | Reverses True and False |
= assigns a value. == compares two values and produces True or False.temperature = 31
is_extreme = temperature < 0 or temperature > 30
print(is_extreme)True
Self-study guide
Comparison operators ask questions and produce Boolean results. Boolean operators combine or reverse those results. Python uses the lowercase keywords and, or and not; OCR Exam Reference Language writes the logical operators as AND, OR and NOT.
A comparison does not normally change data. It evaluates to True or False, which can then be stored, printed or used as the condition in selection or iteration.
OCR Exam Reference Language ↔ Python
| OCR Exam Reference Language / term | Python | What to remember |
|---|---|---|
== | == | Equal to. |
!= | != | Not equal to. |
<, <=, >, >= | <, <=, >, >= | The same comparison symbols are used. |
AND | and | True only when both sides are true. |
OR | or | True when at least one side is true. |
NOT | not | Reverses a Boolean value. |
Worked example
This example creates three Boolean results without using an if statement.
age = 15
member = True
suspended = False
can_enter = age >= 14 and member and not suspended
junior = age < 18
needs_help = not member or suspended
print(can_enter)
print(junior)
print(needs_help)True True False
age >= 14is true.memberis already true.not suspendedis true becausesuspendedis false.- All three parts of
can_enterare therefore true. needs_helpis false because neither side of theoris true.
Another example
Equality comparison is different from assignment.
score = 50
print(score == 50)
print(score != 50)True False
score = 50assigns.score == 50compares.score != 50asks whether the values are different.
Common mistakes
- Using a single
=inside a comparison. - Writing OCR
AND/OR/NOTdirectly into Python instead of lowercaseand/or/not. - Forgetting that
notapplies to the Boolean expression that follows it.
Exam-ready checklist
- Evaluate comparison expressions to True/False.
- Build compound conditions using
and,orandnot. - Translate AND/OR/NOT between OCR ERL and Python.
- Distinguish
=assignment from==equality comparison.
What you have learned
- Comparisons produce Boolean values.
- Use
== != < <= > >=for comparisons. - Use Python
and,orandnotfor Boolean operations. - Do not confuse assignment
=with equality==.