Quarry School

Choose the bounds of range

Subject: Computing
Level 1 · FoundationsAbout 6 min read1 new term
How this was estimated

Programmatic estimate. Rung levels, worked steps, prerequisites, terms and sentence length contribute to this level. It describes the lesson, not your ability.

Reading counts the whole lesson, including folded explanations, at 180 words a minute. Practice allows two minutes per item. These are planning assumptions; take the time you need.

AI estimate unavailable.

Path to here

Open the unit’s lesson list to follow the lesson order.

    Quick review (2 minutes)

    In plain words

    A row of seats is numbered from a first seat up to a sign that says where the row ends.

    Rule

    range(stop) gives the whole numbers from 0 up to but not including stop. range(start, stop) starts at start when start is below stop, and is empty otherwise. range(start, stop, step) moves by step each time and holds the numbers below stop for a positive step, or above stop for a negative one. stop is never included, and a step of 0 raises a ValueError.

    Step

    1. Write the first number you want. That is start. It can be left out when it is 0 and no step is given, because a step needs a start before it.

    2. Choose the step. Leave it out to count up by 1, write a bigger number as the third number to skip, or a negative one to count down.

    3. Write the last number you want, and check that counting from start by the step lands on it; if it does not, change the start or the step. Then set stop past it, no more than one step beyond it: above it when counting up, below it when counting down. For a count up by 1, stop is the last number plus 1. In range(10, 0, -3) the last number is 1, and the stop 0 lies below it, within one step of 3.

    4. Check the count: for a step of 1 and a stop above start, range(start, stop) gives stop minus start numbers.

    5. Write out the numbers the range gives, or note that it gives none, and compare them with what you wanted.

    Trap

    The usual slip is a stop one short. range(1, 5) ends at 4, so a loop meant to count to 5 needs range(1, 6). Write out the last number of the range before you run the program.

    Counting down needs a negative step. range(5, 1) holds no numbers, because its step is 1 and 5 is not below 1. The loop makes no passes and nothing is displayed. Write range(5, 1, -1) for 5, 4, 3 and 2.

    1 In plain words

    A row of seats is numbered from a first seat up to a sign that says where the row ends. The sign itself is not a seat.

    range makes a sequence of whole numbers for a for loop. range(stop) counts 0, 1, 2 and so on, and ends before stop. range(start, stop) counts up from start instead, when start is below stop. A third number, the step, sets how far each number is from the one before, and a negative step counts down.

    The number given as stop is never in the sequence. range(5) gives 0, 1, 2, 3 and 4: five numbers, and 5 is not one of them.

    Remember one sentence: range counts from its start and stops before its stop. To look it up, see section 4.3 of the Python 3.13 tutorial, on the range() function.

    2 Picture it

    CallNumbersrange(5)0 1 2 3 4range(1, 6)1 2 3 4 5range(5, 0, -1)5 4 3 2 1

    A table of three calls of range. range(5) gives 0 1 2 3 4. range(1, 6) gives 1 2 3 4 5. range(5, 0, -1) gives 5 4 3 2 1. Each list starts at the start and moves by the step up to, but not including, the stop, as the rule of this lesson says. The row for range(1, 6) is marked.

    Three calls of range and the numbers each gives

    3 Why

    A range's first number is start, which is 0 when only one number is given, as long as it meets the bound below. Each next number is the one before plus the step, which is 1 when no step is given.

    With a positive step, the sequence holds the numbers below stop. With a negative step, it holds the numbers above stop. So range(1, 6) ends at 5, and range(10, 0, -3) gives 10, 7, 4 and 1: the next number, -2, is not above 0. When the first number already fails that test, as in range(5, 1), the range is empty and the loop makes no passes.

    A step of zero would never move. range refuses it: a step of 0 stops the program with a ValueError.

    4 The rule

    range(stop) gives the whole numbers from 0 up to but not including stop.

    range(start, stop) starts at start when start is below stop, and is empty otherwise. range(start, stop, step) moves by step each time and holds the numbers below stop for a positive step, or above stop for a negative one. stop is never included, and a step of 0 raises a ValueError.

    5 How

    1. Write the first number you want. That is start. It can be left out when it is 0 and no step is given, because a step needs a start before it.
    2. Choose the step. Leave it out to count up by 1, write a bigger number as the third number to skip, or a negative one to count down.
    3. Write the last number you want, and check that counting from start by the step lands on it; if it does not, change the start or the step. Then set stop past it, no more than one step beyond it: above it when counting up, below it when counting down. For a count up by 1, stop is the last number plus 1. In range(10, 0, -3) the last number is 1, and the stop 0 lies below it, within one step of 3.
    4. Check the count: for a step of 1 and a stop above start, range(start, stop) gives stop minus start numbers.
    5. Write out the numbers the range gives, or note that it gives none, and compare them with what you wanted.

    6 Worked example

    Count from 1 to 5

    Python code
    for n in range(1, 6):
        print(n)
    

    Standard input

    Empty

    Standard output

    1
    2
    3
    4
    5
    

    The last number wanted is 5, so stop is 6. Save the code as count.py and run the file.

    Count down by 3

    Python code
    for n in range(10, 0, -3):
        print(n)
    

    Standard input

    Empty

    Standard output

    10
    7
    4
    1
    

    The step is -3, so each number is 3 less than the one before. The trace below shows why the loop ends after 1.

    Number three laps from 1

    Python code
    for lap in range(1, 4):
        print(f"Lap {lap}")
    

    Standard input

    Empty

    Standard output

    Lap 1
    Lap 2
    Lap 3
    

    range(1, 4) gives 1, 2 and 3: three numbers, starting at 1 and stopping before 4.

    Give range a step of zero

    Python code
    for n in range(0, 10, 0):
        print(n)
    

    Standard input

    Empty

    Standard output

    Empty

    Exception ValueError

    Save the two lines as zero.py and run the file. The program stops with a ValueError at the for line, before the body runs once.

    Trace

    Trace the loop over range(10, 0, -3)
    StepnAbove 0?Explanation
    Pass 110yesThe start is 10, and the body displays 10.
    Pass 27yes10 plus the step of -3 is 7.
    Pass 34yes7 minus 3 is 4.
    Pass 41yes4 minus 3 is 1, which is still above the stop of 0.
    Next: -21noThe next number would be -2, which is not above 0. It is not in the range, so the loop ends and n still holds 1.

    7 The trap

    The usual slip is a stop one short. range(1, 5) ends at 4, so a loop meant to count to 5 needs range(1, 6). Write out the last number of the range before you run the program.

    Counting down needs a negative step. range(5, 1) holds no numbers, because its step is 1 and 5 is not below 1. The loop makes no passes and nothing is displayed. Write range(5, 1, -1) for 5, 4, 3 and 2.

    Quick checks

    Quick check 1 of 2

    Which numbers does range(2, 9, 3) give?

    Show the steps and the answer
    Answer

    It gives 2, 5 and 8.

    Why: It starts at 2 and adds 3 each time. The next number, 11, is not below 9, so the range ends at 8.

    Source: Python Software Foundation, Python 3.13 Library Reference, Built-in Types: Ranges

    Quick check 2 of 2

    Write a call of range that gives the whole numbers from 1 to 10, with both 1 and 10 included.

    Show the steps and the answer
    Answer

    range(1, 11)

    Why: The start is 1. The stop is never included, so it is one more than the last number wanted, 10.

    Source: Python Software Foundation, Python 3.13 Tutorial, 4.3 The range() Function

    Sources

    1. Python 3.13 Tutorial, 4.3 The range() Function. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    2. Python 3.13 Library Reference, Built-in Types: Ranges. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    3. Python 3.13 Language Reference, 8.3 The for statement. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    4. Python 3.13 Tutorial, 4.2 for Statements. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.