Python range(): The Stop Value Is Excluded, and Other Essentials

range() generates a sequence of integers — and the first thing to know is that the end value is excluded.

list(range(1, 10)) # [1, 2, 3, 4, 5, 6, 7, 8, 9] ← nine numbers, not ten

That single rule accounts for most off-by-one errors with it, and it’s consistent with the rest of Python — slicing works the same way.

The second thing to know is that range isn’t a list. It’s a lazy object that produces numbers on demand, which is why range(1_000_000) occupies 48 bytes instead of 8 megabytes.

This guide covers the three parameters, what happens at the edges, and when a range is the wrong tool.

The short version
Signaturerange(start, stop, step)
The stop valueExcluded — range(1, 10) ends at 9
Is it a list?No — it’s a lazy object, 48 bytes whatever the size
Only acceptsIntegers — a float raises TypeError
xrangePython 2 only — it doesn’t exist any more

The three parameters

range(stop) range(start, stop) range(start, stop, step)

With one argument, it’s the stop value and counting begins at zero:

list(range(5)) # [0, 1, 2, 3, 4] list(range(1, 6)) # [1, 2, 3, 4, 5] list(range(1, 10, 2)) # [1, 3, 5, 7, 9] list(range(5, 0, -1)) # [5, 4, 3, 2, 1]

⚠️ range(5) gives five numbers starting at 0 — it does not start at 5 and count down. The single argument is where to stop, not where to begin.

And the parameters are positional only:

range(stop=5) # TypeError: range() takes no keyword arguments

⚠️ The stop value is excluded

Three number lines showing that range excludes the stop value: range(1,10) gives nine numbers, range(1,11) gives ten, and range(1,10,2) skips by two

This is the rule that causes most bugs with range().

list(range(1, 10)) # ends at 9 list(range(1, 11)) # ends at 10 ← what you probably wanted list(range(1, 9, 2)) # [1, 3, 5, 7] ← 9 is excluded too

To include the last number, add one to stop.

The behaviour is deliberate and consistent. Slicing works the same way — items[0:5] gives five items — and range(len(items)) produces exactly the valid indices, from 0 to the last one. ⚠️ If the end were inclusive, range(len(items)) would produce an index that doesn’t exist.

Counting down to zero

list(range(10, 0, -1)) # [10, 9, ..., 1] ← stops before 0 list(range(10, -1, -1)) # [10, 9, ..., 1, 0] ← includes 0

The same rule applies going backwards — to reach zero, stop at -1.

What produces an empty range

Three cases, and none of them raises an error:

list(range(10, 1)) # [] stop before start, positive step list(range(0)) # [] nothing to produce list(range(1, 10, -1)) # [] negative step going the wrong way

⚠️ The third is the dangerous one. A negative step with start < stop produces nothing, silently — so a loop over it simply doesn’t run, with no error to explain why.

Two things that do raise:

range(1, 5, 0) # ValueError: range() arg 3 must not be zerorange(1.5) # TypeError: 'float' object cannot be interpreted as an integer

range() works with integers only. For a sequence with decimals, use a list comprehension — [x / 10 for x in range(0, 10)] — or NumPy’s arange().

range is not a list

print(range(1, 6)) # range(1, 6) ← not the numbers print(list(range(1, 6))) # [1, 2, 3, 4, 5]

Printing a range shows the range, not its contents — because the numbers don’t exist yet. They’re generated one at a time, as something asks for them.

Which is why the memory cost is constant:

import syssys.getsizeof(range(1_000_000)) # 48 bytes sys.getsizeof(list(range(1_000_000))) # 8,000,056 bytes

Forty-eight bytes against eight megabytes — and the range would be 48 bytes for a billion numbers too, because it only stores start, stop and step.

⚠️ This is why for i in range(10_000_000) is fine and for i in list(range(10_000_000)) may not be. Only call list() when you genuinely need the list.

It still behaves like a sequence

r = range(10, 20)len(r) # 10 15 in r # True ← computed, not searched r.index(15) # 5 r[2] # 12 r[2:5] # range(12, 15) ← slicing gives another range

⚠️ The membership test is the interesting one. 999_999 in range(1_000_000) is instant — Python calculates whether the number fits the pattern rather than scanning. On a list, the same check walks through a million items.

Using it in a loop

for i in range(5): print(i) # 0 1 2 3 4

This is the right use of range(): when you need the numbers themselves.

⚠️ When you need the items of a collection, don’t use it.

# Writing C in Python for i in range(len(fruits)): print(fruits[i])# Just the items for fruit in fruits: print(fruit)# Items and index for i, fruit in enumerate(fruits): print(i, fruit)

range(len(x)) is the clearest sign of someone bringing another language’s habits. Our guide on for loops covers enumerate() and zip().

Where range genuinely fits

# Repeat something N times, ignoring the counter for _ in range(3): send_retry()# Generate values squares = [x ** 2 for x in range(5)] # [0, 1, 4, 9, 16] sum(range(101)) # 5050# Process in batches for start in range(0, len(records), 100): batch = records[start:start + 100] process(batch)

The underscore is a convention for “I don’t use this variable” — it tells a reader the counter is irrelevant.

⚠️ And the batching pattern is the one worth remembering on a server — it processes a large collection in fixed chunks without loading everything at once.

What xrange was

In Python 2 there were two functions. range() built a real list, and xrange() produced numbers lazily.

Python 3 removed range() and renamed xrange() to range() — so today’s range behaves like the old xrange.

xrange(5) # NameError: name 'xrange' is not defined

⚠️ There is no choice to make between them. Material comparing range with xrange, or calling one a class and the other a function, is describing Python 2 — which reached end of life in January 2020.

And range is a type, not a function:

type(range) # <class 'type'> type(range(5)) # <class 'range'>

Calling it creates an object of that type — there’s no separate function version anywhere.

⚠️ range on a server

One pattern matters, and it’s the batching one.

A script that loads a hundred thousand database rows into memory at once will hit the account’s memory limit — and on shared hosting, the process is killed rather than raising, so the log is empty.

for offset in range(0, total, 500): rows = fetch(limit=500, offset=offset) process(rows)

Five hundred rows in memory at a time, regardless of how many there are in total.

⚠️ And range() itself is never the memory problem — 48 bytes for any size. What costs memory is what you build from it, which is why list(range(n)) on a large n is the thing to avoid.

Batching exists because memory limits do

A script that loads everything at once gets killed by the account’s RAM cap — with no traceback, because the process was terminated rather than raised. Copahost VPS plans give you dedicated memory, root access, and logs that actually say what happened. From €3.99/month, with snapshots.

See VPS plans

Frequently asked questions

Why does range(1, 10) stop at 9?

Because the stop value is excluded — it’s where the sequence ends, not the last item. To include 10, write range(1, 11). The behaviour matches slicing, where items[0:5] also gives five items.

Does range(5) start at 5?

No. With a single argument, that argument is the stop value — range(5) gives [0, 1, 2, 3, 4], starting at zero. To start elsewhere, pass two arguments: range(5, 10).

Why does print(range(5)) show range(0, 5) instead of the numbers?

Because a range doesn’t hold numbers — it generates them on demand. Printing it shows the object. Wrap it in list() to see the values.

Is range a list in Python?

No. It’s a lazy object storing only start, stop and step. range(1_000_000) occupies 48 bytes; the equivalent list occupies about 8 megabytes — and the range would still be 48 bytes for a billion numbers.

Can I use range() with decimals?

No — range(1.5) raises TypeError. It accepts integers only. For decimal steps, use a list comprehension like [x / 10 for x in range(0, 10)], or NumPy’s arange().

How do I count down with range()?

With a negative step: range(10, 0, -1) gives 10 down to 1. ⚠️ To reach zero, stop at -1 — range(10, -1, -1) — because the stop value is excluded going backwards too.

Why is my range empty?

Three causes, none of which raises. Stop before start with a positive step — range(10, 1). A stop of zero — range(0). Or a negative step going the wrong way — range(1, 10, -1). ⚠️ The loop simply doesn’t run, with no error to explain it.

Does xrange still exist?

No. It was Python 2 only. Python 3 removed the old list-building range() and renamed xrange() to range() — so today’s range is the lazy one. Any comparison between the two describes a version that reached end of life in January 2020.

Should I use range(len(items)) to loop over a list?

No. Use for item in items: when you need the items, or enumerate(items) when you need the index too. range(len()) works, but it’s the clearest sign of writing Python with another language’s habits.

Can I slice a range?

Yes, and the result is another range: range(10)[2:5] gives range(2, 5). It stays lazy — no list is created unless you ask for one.

Is x in range(n) slow for large n?

No — it’s instant. Python calculates whether the number fits the start, stop and step pattern rather than scanning. ⚠️ On a list, the same check walks through every item.

How do I process a large collection in batches?

With a step equal to the batch size: for start in range(0, len(records), 100):, then slice records[start:start + 100]. It keeps a fixed number of items in memory regardless of the total.

Conclusion

range() generates integers, and two facts cover almost everything about it.

The stop value is excluded — range(1, 10) ends at 9, and adding one to stop is how you include the last number. This is consistent with slicing, and it’s what makes range(len(items)) produce exactly the valid indices.

And a range isn’t a list. It stores three numbers and generates the rest on demand, which is why range(1_000_000) costs 48 bytes and the equivalent list costs 8 megabytes. ⚠️ list(range(n)) on a large n is the thing to avoid — the range itself never is.

Two more worth carrying. An empty range raises nothing — a negative step going the wrong way just makes the loop not run. And xrange doesn’t exist; any material comparing it with range is describing Python 2, which ended in January 2020.

Share the Post:
Picture of Gustavo Gallas

Gustavo Gallas

Graduated in Computing at PUC-Rio, Brazil. Specialized in IT, networking, systems administration and human and organizational development​. Also have brewing skills.