Combine Conditions

Combine small comparison questions with and, or, and not. Express one ordinary interval clearly and check an equivalent chained comparison by hand.

One comparison can ask whether a reading crosses one threshold. A useful classification sometimes needs two boundaries. For example, suppose ordinary readings are at least 18 and below 22.

The output is:

True

Require Both Parts of an Interval

and joins two questions. The whole condition is true only when both parts are true. For a reading of 20, reading >= 18 is true and reading < 22 is true, so is_ordinary is true.

At a boundary, trace the parts separately:

readingreading >= 18reading < 22is_ordinary
17FalseTrueFalse
18TrueTrueTrue
21TrueTrueTrue
22TrueFalseFalse

The name is_ordinary tells us what the full condition means. It is more helpful than scattering the two comparisons wherever the program needs them.

Q1. Check an interval boundary

For reading = 22, what value does this expression produce?

reading >= 18 and reading < 22
Choose one

Select one choice, then check.

HintTest both sides

Do not stop after reading >= 18. Also test reading < 22.

SolutionThe interval excludes 22

Although 22 meets the lower boundary, it fails the upper one. One false part makes the and condition false.

Not attempted
Review

Not marked done.

Accept Either Outside Case

or joins alternatives. A reading needs attention when it is below 18 or at least 22:

needs_attention = reading < 18 or reading >= 22

This condition is true when either outside case is true. It is false only for readings that are neither low nor high. The two conditions describe the same classification from opposite directions:

For a known reading, write down each comparison result before trusting the combined answer. That small hand trace catches reversed operators and missing boundary values.

Q2. Choose the outside test

Which condition is true for a reading of 17 or 23, but false for a reading of 20?

Choose one

Select one choice, then check.

HintThere are two outside directions

One comparison should cover values below 18 and the other values from 22 upward.

SolutionUse or for the two outside cases

reading < 18 or reading >= 22 is true for a low reading and for a high reading. A middle reading satisfies neither part.

Not attempted
Review

Not marked done.

Reverse One Boolean Answer

not reverses a boolean result. If a name already captures a useful question, not can express its opposite without rewriting that question:

When is_high is True, is_not_high is False; when is_high is False, is_not_high is True. Use not when the resulting condition still reads plainly. A name such as needs_attention is often clearer than several layers of negation.

Python also permits the interval to be written as one chained comparison:

is_ordinary = 18 <= reading < 22

This asks the same two boundary questions as the earlier and expression. Use the form that makes the interval easiest to inspect. Both forms deserve the same boundary check.

Q3. Read a reversed boolean

If is_high is True, what is the value of not is_high?

Choose one

Select one choice, then check.

HintReverse the answer

Start with the known boolean value, then take its opposite.

Solutionnot True is False

Since is_high is True, not is_high is False.

Not attempted
Review

Not marked done.

and, or, and not let a program express a readable combined question. We can now classify one reading clearly; next we will keep several readings together instead of giving each one a separate name.

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.