Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

Why does `range(1, 5)` stop before 5 in a Python loop?

Quick answer

range(1, 5) produces 1, 2, 3, 4: the start is included, but the stop is excluded. If you want to count from 1 through 5, use range(1, 6). For a simple upward count by one, set the stop one greater than the last value you want included. Think of the stop as a boundary, not the final value in the sequence.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

Why does range(1, 5) stop before 5? I expected the loop to print 1 through 5.

Geez

In range(1, 5), the start value is included and the stop value is excluded. The resulting values are 1, 2, 3, 4, so a loop over that range does not process 5. [S1]

For example:

for number in range(1, 5):
    print(number)

The loop prints:

1
2
3
4

The stop is a boundary for where the range ends, not the last value to include.

Follow-up question

How do I change it so the loop includes 5, and how do I choose the stop?

Geez

For a count from 1 through 5, use 6 as the stop:

for number in range(1, 6):
    print(number)

The values are 1, 2, 3, 4, 5. Python’s tutorial gives range(1, 6) as the way to include 5. [S1]

For a simple count upward by one, write down the last value you want, then make the stop one greater. For example, to include 8 when starting at 1, use range(1, 9). This rule describes that straightforward upward count; if you change the step or direction, work out the intended sequence rather than applying the rule automatically.

Follow-up question

Is the excluded stop related to list slicing? I want a mental model that won’t mislead me.

Geez

The range example gives you a useful boundary idea: the stop marks where the values end, rather than naming the final included value. Python’s tutorial also notes that ranges are useful for sequence indices, which end one position before the sequence length. [S1]

That may be a helpful starting intuition when you encounter slice notation, but it does not mean ranges and slices necessarily follow identical rules. Learn slicing on its own when you need its specific behavior; this range example only establishes how the range’s stop works.

Follow-up question

What’s a quick way to check whether I’ve chosen the right endpoint for a different loop?

Geez

First identify the last value you want the loop to process. For a simple count upward by one, set the stop one greater than that value. If you want to include 2 through 7, for example:

for number in range(2, 8):
    print(number)

The range produces 2, 3, 4, 5, 6, 7; it excludes the stop value, 8. [S1] Before using a range, check that its first value and final included value match your intention. The important distinction is between the last value you want and the boundary you provide as the stop.

A practical checklist

  • Write down the first value the loop should process.

  • Write down the last value you want included.

  • For an upward count by one, use a stop one greater than that last value.

  • Check that the range’s first and final included values match your intention.

  • If you use a different step or direction, work out the intended sequence rather than applying the simple +1 rule automatically.

A few more useful answers

What values does `range(1, 5)` produce?

It produces 1, 2, 3, 4; the stop value, 5, is excluded. [S1]

How do I include 5 in a range that starts at 1?

Use range(1, 6). This includes 5 while keeping the stop value excluded. [S1]

Does this explain all the rules for Python list slicing?

No. It explains the range stop and offers a boundary intuition, but does not establish slice behavior. Learn slicing separately when you need its specific rules.

Sources

References used for this guide.

  1. [S1] Python Tutorial: The range() function
    docs.python.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work