Python range() Explained: How to Create Number Sequences

Python range() creates a lazy sequence of integers, most often for for loops. It does not build a full list unless you ask it to, which makes it efficient for counting, stepping, and repeating work a known number of times. The rest of this article shows how the three forms of range() work, why it stops before the end value, and how to avoid the mistakes beginners make.

Difficulty: Beginner

Helpful to know first: basic Python variables, for loops, and how integer values work.

How Python range() Creates a Sequence of Integers

range() gives you numbers one by one, in order, without storing them all in memory at once. That is why it is a good fit for loops: Python can ask for the next number only when it needs it.

The simplest form takes one argument. That argument is the stop value, and the sequence starts at zero.

Python
for number in range(5):
    print(number)

# Output:
# 0
# 1
# 2
# 3
# 4

This prints five numbers: 0 through 4. The number 5 is not included, because range() stops before the end value.

You can think of it as “start here, count by this step, stop before reaching that value.” The default start is 0, and the default step is 1.

Using range() with start, stop, and step

When you give range() two or three arguments, you control where the sequence begins and how it moves.

Starting from a number other than zero

If you want to count from 3 to 7, pass both the start and stop values:

Python
for number in range(3, 8):
    print(number)

# Output:
# 3
# 4
# 5
# 6
# 7

The stop value is 8, not 7, because range() does not include the stop value itself.

Skipping numbers with step

The third argument controls the step size. A step of 2 skips every other number.

Python
for number in range(0, 10, 2):
    print(number)

# Output:
# 0
# 2
# 4
# 6
# 8

This is useful for even numbers, table rows, and any loop that should advance in fixed jumps.

Counting backward with a negative step

range() also works in reverse when the step is negative.

Python
for number in range(5, 0, -1):
    print(number)

# Output:
# 5
# 4
# 3
# 2
# 1

Here the stop value is still excluded, so the loop ends before 0. That is the same rule as forward counting.

Real examples of range() in everyday Python code

Running something a fixed number of times

If you only care about repetition, range() is the cleanest tool.

Python
for _ in range(3):
    print("Hello")

# Output:
# Hello
# Hello
# Hello

The underscore _ tells readers that the loop variable is not used. Python still creates the sequence, but your code shows that the value itself does not matter.

Looping over list indexes

Sometimes you need the position as well as the value. range(len(...)) is one common way to do that.

Python
names = ["Ada", "Linus", "Grace"]

for index in range(len(names)):
    print(index, names[index])

# Output:
# 0 Ada
# 1 Linus
# 2 Grace

This pattern is useful when you need to update items by position. If you only need the values, looping directly over the list is usually clearer.

Processing every other item

With a larger step, range() can skip through a sequence efficiently.

Python
letters = ["a", "b", "c", "d", "e", "f"]

for index in range(0, len(letters), 2):
    print(letters[index])

# Output:
# a
# c
# e

This is often easier than manually incrementing a counter inside the loop.

Counting down with a countdown

Reverse ranges are useful for timers, reverse loops, and working from the end of a sequence toward the beginning.

Python
for seconds in range(3, 0, -1):
    print(seconds)

print("Go!")

# Output:
# 3
# 2
# 1
# Go!

The loop stops before zero because the stop value is exclusive. If you want to include zero, you need a different stop value.

How range() behaves in detail

Two details matter most: the stop value is excluded, and the step cannot be zero. Those two rules explain most beginner mistakes.

range() does not build a list immediately. It produces a range object that can be iterated over, and Python calculates each next integer as needed. That makes it memory-efficient even for very large ranges.

You can still convert it to a list if you want to see the values all at once:

Python
numbers = list(range(5))
print(numbers)

# Output:
# [0, 1, 2, 3, 4]

That conversion is convenient for teaching or debugging, but in real loops you usually do not need it.

If the step is positive, the sequence moves forward and stops before reaching or passing the stop value. If the step is negative, the sequence moves backward and stops before reaching or passing the stop value in reverse.

The stop value is not a “last value.” It is a boundary. That is why range(5) gives 0 through 4, not 0 through 5.

A zero step is invalid because the sequence would never move. Python rejects that immediately.

Terminal
# range(1, 5, 0)
# ValueError: range() arg 3 must not be zero

The values in a range() are integers only. If you need decimal steps, use a different approach such as a while loop or decimal.Decimal calculations.

Predict the Output

Does the stop value appear?

Python
for number in range(1, 4):
    print(number)

What does this print?

Show answer

It prints 1, 2, and 3. It does not print 4 because the stop value is excluded.

Terminal
# Output:
# 1
# 2
# 3

What happens with a negative step?

Python
for number in range(3, -1, -1):
    print(number)

What does this print?

Show answer

It prints 3, 2, 1, and 0. The stop value is -1, and the loop stops before reaching it.

Terminal
# Output:
# 3
# 2
# 1
# 0

Common Mistakes with range()

Assuming the stop value is included

Many beginners write the upper bound they want to see, then wonder why the last number is missing. This is the most common range() mistake.

Problem: The loop stops before 5, so the final value never appears.

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

Fix: Use one more than the last number you want to print.

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

The corrected version works because range() includes the start value but excludes the stop value.

Using a step of zero

A step of zero cannot move forward or backward, so Python raises an error instead of creating an infinite loop.

Problem: The third argument is zero, and range() cannot advance with no step.

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

Fix: Use a positive or negative step that actually changes the value.

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

The corrected version works because the step now moves the sequence forward.

Using the wrong direction for the step

If the start is smaller than the stop, a negative step gives no values. The same is true in the other direction with a positive step.

Problem: The sequence starts at 1 and tries to move down, but the stop value is above it, so nothing happens.

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

Fix: Match the step direction to the direction you want the numbers to move.

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

The corrected version works because the step direction and the start-to-stop direction agree.

Turning range() into a list when you do not need one

Beginners sometimes write list(range(...)) out of habit. That is fine for small examples, but it creates a full list in memory.

Problem: The code allocates a list of every value even though a simple loop only needs to visit them once.

Python
numbers = list(range(1000000))
for number in numbers:
    pass

Fix: Loop directly over range() unless you truly need a list.

Python
for number in range(1000000):
    pass

The corrected version works because it avoids building an extra container.

Find the Bug

A hurried developer wants to print the even numbers from 2 through 10. Find the bug in this code.

Python
for number in range(2, 10, 2):
    print(number)
Show answer

The bug is the stop value. range(2, 10, 2) prints 2, 4, 6, and 8, but it does not include 10.

Fixed code:

Python
for number in range(2, 11, 2):
    print(number)

# Output:
# 2
# 4
# 6
# 8
# 10

The fixed version works because the stop value is one past the last even number you want.

When to use range() and when to use something else

range() is the right choice when you need a predictable sequence of integers. It is not the right choice for decimal stepping, dates, or values that come from a file or network response.

Use range()Use something else
You need counting, indexes, or fixed repetition.You need values from an existing collection or stream.
You want integer steps.You need decimal or fractional steps.
You want a lightweight loop sequence.You need dates, times, or custom objects.

For example, if you want to loop over list values directly, this is usually clearer than looping over indexes:

Python
for name in ["Ada", "Linus", "Grace"]:
    print(name)

# Output:
# Ada
# Linus
# Grace

Use range() only when the index itself matters or when you need a numeric pattern.

Common interview questions about range()

Why does Python exclude the stop value?

It makes ranges easier to reason about and helps adjacent ranges fit together cleanly. For example, range(0, 5) and range(5, 10) do not overlap.

Is range() a list?

No. range() returns a range object, which is an iterable sequence of integers. You can convert it to a list, but the object itself is not a list.

Can range() be very large?

Yes. A range object can represent large sequences efficiently because it does not store every integer as a separate list element.

Try It Yourself

Create a loop that prints the numbers 10 down to 1, one per line.

  • Use range() with three arguments.
  • The output should include both 10 and 1.
  • Do not build a list first.

Expected output:

Terminal
# 10
# 9
# 8
# 7
# 6
# 5
# 4
# 3
# 2
# 1

Hint: Start at 10, stop one past the last number you want, and use a negative step.

Show solution
Python
for number in range(10, 0, -1):
    print(number)

Summary

  • range() creates a sequence of integers for loops and counting.
  • range(stop) starts at 0 and stops before stop.
  • range(start, stop, step) lets you control the beginning, end boundary, and increment.
  • The stop value is always excluded, which is why off-by-one mistakes are so common.
  • A step of zero is invalid, and negative steps count backward.
  • Use range() for integer sequences; use a different tool for decimals or non-integer values.

Next, learn enumerate() if you want both the index and value from a loop without writing range(len(...)) yourself.