06-comparison-boolean-operators.py
python-programming / programming-foundations  ·  Lesson 06
Lesson 06 — Programming foundations

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.

comparison-boolean-operators-predict.py
age = 15
print(age >= 13 and age <= 19)
print(age == 16)
print(not age < 13)
Predict before running
  • Predict the three Boolean outputs.
  • Which expression uses AND?
  • What does not do to a Boolean result?

Investigate

Use this code to explain exactly how the program works.

comparison-boolean-operators-investigate.py
score = 72
submitted = True
print(score >= 70 and submitted == True)
print(score < 40 or submitted == False)
Investigate the code
  • Break each line into its individual comparisons.
  • Evaluate each comparison first, then combine the results.
  • Why is == used instead of =?

Comparison operators

MeaningGeneral formPython
Equal to====
Not equal to!=!=
Less than<<
Less than or equal<=<=
Greater than>>
Greater than or equal>=>=

Boolean operators

General formPythonMeaning
ANDandTrue only when both conditions are True
ORorTrue when at least one condition is True
NOTnotReverses True and False
= is not === assigns a value. == compares two values and produces True or False.
conditions.py
temperature = 31
is_extreme = temperature < 0 or temperature > 30
print(is_extreme)
Output
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 focusRecognise and use all six comparison operators plus AND, OR and NOT. The comparison symbols are the same in Python; the Boolean operator words change case.

OCR Exam Reference Language ↔ Python

OCR Exam Reference Language / termPythonWhat to remember
====Equal to.
!=!=Not equal to.
<, <=, >, >=<, <=, >, >=The same comparison symbols are used.
ANDandTrue only when both sides are true.
ORorTrue when at least one side is true.
NOTnotReverses a Boolean value.
Read both formsIn an examination you may see OCR Exam Reference Language even when you have learned the idea in Python. Translate the structure, not just the symbols.

Worked example

This example creates three Boolean results without using an if statement.

lesson-06-worked.py
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)
Output
True
True
False
Work through it line by line
  • age >= 14 is true.
  • member is already true.
  • not suspended is true because suspended is false.
  • All three parts of can_enter are therefore true.
  • needs_help is false because neither side of the or is true.

Another example

Equality comparison is different from assignment.

lesson-06-example-2.py
score = 50
print(score == 50)
print(score != 50)
Output
True
False
Why this example matters
  • score = 50 assigns.
  • score == 50 compares.
  • score != 50 asks whether the values are different.

Common mistakes

Avoid these errors
  • Using a single = inside a comparison.
  • Writing OCR AND / OR / NOT directly into Python instead of lowercase and / or / not.
  • Forgetting that not applies to the Boolean expression that follows it.
Exam-safe warningPython Boolean keywords are lowercase. OCR ERL uses uppercase operator names.

Exam-ready checklist

You should now be able to
  • Evaluate comparison expressions to True/False.
  • Build compound conditions using and, or and not.
  • 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, or and not for Boolean operations.
  • Do not confuse assignment = with equality ==.