thecodezone.
The Code Zone
Short steps, at your pace · Free lessons and practice · Member video walkthroughs
Save your next small win.

Checking your account…

01.08 · Conditions and decisions / Watch · 9 min

Check both ends of a range

A range has two ends. Your comparison must say whether each end is included.

Activity 8 of 9 in Conditions and decisions

All courses

Python 2: Logic, Loops & Functions / 01.08

Understand chained comparisons

Not started

Read a chained comparison as two connected questions and decide whether each endpoint belongs.

More options

Your lesson progress

Walkthrough: not startedPractice: not started

Empty: not started. Yellow: in progress. Green: complete. Marking the whole activity complete does not replace your test results. These circles save in this browser for this pilot.

Rob places temperature 22 between inclusive endpoints 15 and 25, then sees that the chained comparison is True.
Rob’s comic strip · Conditions and decisions

ORIGINAL WALKTHROUGH · 2:36

Watch with Rob.

Pause whenever you want to try a change. You can return to the lesson at any time.

LEARN WITH THE CODE ZONE

Unlock this video walkthrough

Written lessons and coding practice are free. Video walkthroughs are included with a paid Code Zone membership or an active free trial.

Have a Code Zone website account? Use your learning account’s email to get a six-digit sign-in code, or use your existing website password.

Need a website account? Paid Skool members and active trial members can use the email from their membership questions to request a sign-in code. No invite link or Skool password needed.

Close this window to keep practicing. After signing in, you’ll return to this lesson.

Video walkthroughs are included with an active Code Zone membership. The written lesson and practice are free.

Open video & code editor (opens in a new tab)

Starter source code for VS Code

Copy the source below or download the file to follow along in VS Code.

The download includes a lesson link in each file and a README so you can find your way back.

main.py
main.pypython · example
# Code along with Rob: chained comparisons
# Compare lower <= value <= upper with two comparisons joined by and.
# Decide whether each endpoint belongs.
# Try inside, exact endpoint and outside values.

The middle value belongs to both comparisons

15 <= temperature <= 25 asks two questions: is 15 less than or equal to temperature, and is temperature less than or equal to 25? Both must hold for the chain to be True. Read it as a range, not as one comparison producing a value to compare again.

The two forms below agree for temperature 22. Python evaluates the middle expression only once in a chain, and skips later comparisons if an earlier one is false. With a plain variable, the expanded and form is an easy way to understand the rule.

Two questions about the same middle value
range_check.pypython · example
temperature = 22

print(15 <= temperature <= 25)  # True
print(15 <= temperature and temperature <= 25)  # True
Open workspace to paste this example (opens in a new tab)

Choose each endpoint deliberately

<= includes equality. < excludes it. Our example comfortable range includes both 15 and 25; that is a lesson rule rather than weather advice. The first two lines below test the endpoints themselves.

Check an inside value and values just outside both ends. If you only try 22, both an inclusive and an exclusive version pass, so that single example cannot reveal an endpoint mistake.

The endpoints are test cases too
range_check.pypython · example
print(15 <= 15 <= 25)  # True
print(15 <= 25 <= 25)  # True
print(15 <= 14 <= 25)  # False
print(15 <= 26 <= 25)  # False
Open workspace to paste this example (opens in a new tab)

Mixed endpoints describe a different rule

Suppose a practice schedule is open from hour 8 up to, but not including, hour 17. The lower endpoint is inclusive and the upper endpoint is exclusive. Use 8 <= hour < 17 to express that exact rule.

At hour 17, the result is False. At hour 8, it is True. Do not automatically use the same operator at both ends; read what the rule says.

Include the opening hour, exclude the closing hour
schedule.pypython · example
hour = 17
is_open = 8 <= hour < 17
print(is_open)  # False

hour = 8
is_open = 8 <= hour < 17
print(is_open)  # True
Open workspace to paste this example (opens in a new tab)

Keep the comparisons readable

Chaining works naturally for ordered bounds. For unrelated questions, use and with clear variable names instead of forcing everything into one chain. Do not assume that a chained comparison means all values are different or that it works like JavaScript.

In the next practice, fix the missing upper endpoint in the source temperature-range task. Predict the result for 14, 15, 22, 25 and 26 before you run it.

Build it one step at a time

Prefer learning by typing? Work through short instructions, run the Python code and check each change before continuing. Your guided project saves separately from the challenge below.

The mini-lesson opens in a new tab. Your own code carries forward; each step has a focused check and an optional hint.

Start mini-lesson(opens in a new tab)

3 steps · Saved work · Instant feedback

YOUR TURN

Include both temperature limits

Keep temperature = 25. Use one chained comparison for the inclusive range 15 through 25. Store its Boolean answer in is_comfortable and keep the single print call.

Expected output
True

Keep the starting inputs. Print these lines in this order, without extra output.

  • Keep temperature at the upper endpoint 25
  • Store the Boolean True in is_comfortable

Edit Python, then run it. Check challenge checks your output and the listed Python requirements. Your code saves on this browser after a short pause. Sign in and choose Save account draft to keep it across devices. Word wrap is enabled.

Ready to checkRun the checks to see your results here.

Need a nudge?

Equality belongs at both limits

The left end already includes 15. Compare < and <= at the right end: which includes a temperature equal to 25?

Replace your editor code?

Your current work will be replaced. Choose Cancel to keep it.

Ready to try it?

Practice in your browser and see your results.

Progress details

Have a question?

Ask the Code Zone community.

Ask a question in The Code Zone (opens in a new tab)

Continue learning

ONE STEP FORWARD

A range check is only correct when both endpoint operators match the rule. Next, fix an excluded upper endpoint.