Ask True-or-False Questions

Use comparisons to ask precise questions about one reading and predict the boolean result. Distinguish assignment from equality and test the exact boundary values.

The measurement report can calculate a mean, but a program often needs to ask a question about one reading before it decides what to do next. For example, is a reading high enough to inspect? Does it equal the expected value?

Python can answer such questions with one of two values: True or False. These values are called booleans.

Compare a Reading with a Threshold

Suppose readings of 22 or more need attention. A comparison asks Python to compare two values:

The output is:

True

>= means “greater than or equal to.” Python reads the current value of reading, compares 22 with 22, and assigns the boolean result to is_high. The name states the question, which makes later code easier to read.

The comparison operators used with numbers are:

QuestionPython operatorExample when reading is 20Result
Is it less than 20?<reading < 20False
Is it at most 20?<=reading <= 20True
Is it greater than 20?>reading > 20False
Is it at least 20?>=reading >= 20True
Is it exactly 20?==reading == 20True
Is it different from 20?!=reading != 20False

An exact boundary matters. If the threshold is 22, reading >= 22 is true for 22 itself. reading > 22 would give a different answer at that boundary.

Q1. Predict a threshold question

What does this program display?

Choose one

Select one choice, then check.

HintRead the operator carefully

>= includes the threshold, but 21 is still below 22.

SolutionThe result is False

The comparison asks whether 21 is at least 22. It is not, so is_high refers to False.

Not attempted
Review

Not marked done.

Assignment Is Not Equality

Chapter 2 used = for assignment:

reading = 20

This tells Python to make reading refer to 20. A comparison uses two equal signs:

is_ordinary = reading == 20

This asks whether the current value of reading is exactly 20. One equal sign changes what a name refers to. Two equal signs ask a question and produce a boolean. Mixing them up is a common early mistake, so pause long enough to count the signs.

Q2. Choose the equality question

Which line asks whether reading is exactly 20 without changing reading?

Choose one

Select one choice, then check.

HintCount the equal signs

The line must produce True or False and leave reading unchanged.

SolutionUse == for equality

reading == 20 compares the current reading with 20. It does not assign a new value to either name.

Not attempted
Review

Not marked done.

Give the Question a Useful Name

A boolean name should read like the question it records:

The output is:

True
False

Here is_ordinary records one exact expectation: a reading of 20. It does not yet describe a whole interval of ordinary readings. We will combine simple questions later, after we can make the program choose a response.

Q3. Check the exact boundary

For reading = 22, what are the values of is_high and is_ordinary in this code?

Choose one

Select one choice, then check.

HintUse the two different tests

First test whether 22 is at least 22. Then test whether it is equal to 20.

SolutionThe results are True and False

22 >= 22 is True because the operator includes equality. 22 == 20 is False because the two values differ.

Not attempted
Review

Not marked done.

A comparison produces True or False; a well-named boolean makes the question visible in the program. The next lesson uses that answer to choose one response for a reading.

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 3 exercises marked done

Review

Not marked done.