Python range() Function
range(start, stop, step) produces a sequence of integers from start up to, but not including, stop; with one argument, range(n) gives 0 to n - 1.range() is the standard way to repeat something a set number of times or to walk through index positions in a for loop. It does not build a big list in memory. It stores only start, stop and step and works out each number when asked, so range(1_000_000) is just as cheap to create as range(5).
The range lesson walks through more loop patterns.
| Syntax | range(stop), range(start, stop) or range(start, stop, step) |
|---|---|
| Parameters | start: first number (default 0); stop: end, never included; step: gap between numbers (default 1, never 0) |
| Returns | A range object, an immutable sequence of integers |
| Arguments | Must be integers (or objects with __index__); floats raise TypeError |
How start, stop and step work in range()
The three forms behave like this:
range(5)gives0, 1, 2, 3, 4.range(2, 6)gives2, 3, 4, 5.range(0, 10, 3)gives0, 3, 6, 9.range(5, 0, -1)gives5, 4, 3, 2, 1, so a negative step counts down.
The stop value is always excluded. That is why range(len(items)) produces exactly the valid indexes of a list.
print(list(range(1, 6)))
print(list(range(10, 0, -3)))
print(list(range(5, 1)))The last line prints an empty list: when start is already past stop for the direction of step, there is nothing to produce.
range() is a lazy sequence, not a list
A range object supports len(), indexing, slicing, in and iteration. It is not a list, so print(range(3)) shows range(0, 3). Wrap it in list() when you want to see or modify the numbers. Membership tests such as 50 in range(0, 100) are fast because Python calculates the answer instead of scanning.
r = range(0, 20, 5)
print(r)
print(len(r), r[2], 10 in r, 11 in r)range() vs enumerate() for looping with indexes
for i in range(len(names)) works, but when you need both the position and the value, enumerate() is shorter and less error-prone. Keep range() for counting, repeating and generating numbers.
Examples you can run
Press Run, then edit the code and run it again.
Repeat an action a fixed number of times
The loop variable counts from 1 to 3 because stop is 4 and excluded.
for attempt in range(1, 4):
print("Attempt", attempt)Expected outputAttempt 1
Attempt 2
Attempt 3
Step values and countdowns
Use step for even numbers, multiples or counting backwards.
print(list(range(0, 11, 2)))
print(list(range(3, 0, -1)))
print(sum(range(1, 101)))Expected output[0, 2, 4, 6, 8, 10]
[3, 2, 1]
5050
Loop over list positions
A realistic use: compare each score with the one before it.
scores = [60, 75, 70, 90]
for i in range(1, len(scores)):
change = scores[i] - scores[i - 1]
print(i, change)Expected output1 15
2 -5
3 20
Common mistakes
Passing a float
✗ Wrong
for i in range(2.5):
print(i)✓ Right
for i in range(int(2.5)):
print(i)Convert to an integer first, for example range(int(2.5)), or use round().
Expecting the stop value to be included
✗ Wrong
print(list(range(1, 5)))✓ Right
print(list(range(1, 6)))stop is excluded. Use range(1, 6) to include 5.
Using a step of zero
✗ Wrong
print(list(range(0, 10, 0)))✓ Right
print(list(range(0, 10, 2)))The step must be a non-zero integer. Use a negative step to count down.
Python range() Function: questions people ask
Does range() include the last number?
No. range(1, 5) stops before 5 and gives 1, 2, 3, 4. Raise the stop value by one to include the number you want.
How do I loop backwards with range()?
Use a negative step, such as range(10, 0, -1). The start must be larger than the stop for it to produce anything.
Can range() use decimal numbers?
No, range() accepts only integers. For decimal steps, compute values yourself, for example [i * 0.5 for i in range(5)].
Keep learning
Step-by-step lessons:
Python range() FunctionPython for Loops
Related:
Python len() FunctionPython enumerate() FunctionPython sum() FunctionPython reversed() Function
More in built-in functions:
Python print() FunctionPython len() FunctionPython type() FunctionPython int() FunctionPython float() FunctionPython str() FunctionPython input() FunctionPython sum() Function See all →