Python Reference Library →

Python range() Function – Generate Number Sequences Easily

Let’s look at range() function – generate number sequences easily

  1. Notice

    range() represents an arithmetic sequence of integers. Its stop value is excluded, and it does not store every integer as a list.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
numbers = range(3)
print(list(numbers))
Live state

Top-level scope

numbers→Object 1
Object 1 · rangerange(0, 3)
This step

range() represents an arithmetic sequence of integers. Its stop value is excluded, and it does not store every integer as a list.

Picture range() function – generate number sequences easily

range() represents an arithmetic sequence of integers. Its stop value is excluded, and it does not store every integer as a list.

Image brief

Alt text
A diagram for Python range() Function – Generate Number Sequences Easily. Instructions: numbers = range(3) print(list(numbers)). Displayed result: [0, 1, 2]. The final example bindings are numbers --> object 1 (range). object 1: range(0, 3)
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python range() Function – Generate Number Sequences Easily". Teaching focus: range() represents an arithmetic sequence of integers. Its stop value is excluded, and it does not store every integer as a list. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS numbers = range(3) print(list(numbers)) | v DISPLAYED RESULT [0, 1, 2] FINAL BINDINGS numbers --> object 1 (range) object 1: range(0, 3) Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python range() Function – Generate Number Sequences Easily. Instructions: numbers = range(3) print(list(numbers)). Displayed result: [0, 1, 2]. The final example bindings are numbers --> object 1 (range). object 1: range(0, 3)

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
numbers = range(3)
print(list(numbers))
Live state

Top-level scope

numbers→Object 1
Object 1 · rangerange(0, 3)
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
[0, 1, 2]

What do you expect this program to print?

What does this example display?

numbers = range(3)
print(list(numbers))
What does this example display?
Choose one answer. You can try again.

Answer and explanation

[0, 1, 2]

Follow the printed values in order. The output is: [0, 1, 2]

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

range() represents an arithmetic sequence of integers. Its stop value is excluded, and it does not store every integer as a list.

range() represents an arithmetic sequence of integers. Its stop value is excluded, and it does not store every integer as a list.

More examples and reference

Python range() Function

The range() function in Python is used to generate a sequence of numbers. It's commonly used in for loops to repeat a block of code a specific number of times.

Syntax

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

Parameters:

  • start (optional) – The starting number of the sequence. Defaults to 0.
  • stop – The number at which to stop (not included).
  • step (optional) – The difference between each number in the sequence. Defaults to 1.

Returns:

An immutable sequence type of numbers.

Example 1: Using range(stop)

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

Example 2: Using range(start, stop)

for i in range(2, 6):
    print(i)
2
3
4
5

Example 3: Using range(start, stop, step)

for i in range(1, 10, 2):
    print(i)
1
3
5
7
9

Example 4: Reversed Sequence with Negative Step

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

Use Cases

  • Looping over a fixed number of times
  • Generating indices for list access
  • Iterating in reverse
  • Creating sequences for algorithms

Convert to List (for Viewing)

print(list(range(1, 6)))
[1, 2, 3, 4, 5]

Using list() helps you visualize the numbers generated by range().

Common Mistakes

  • Forgetting that stop is exclusive (not included)
  • Using a step of 0 will raise a ValueError
  • Assuming range() returns a list – it returns a special range object

Interview Tip

range() is often used in loop-based questions. Practice customizing start, stop, and step to write optimized code.

Summary

  • range() generates numbers from start to stop - 1
  • It’s memory-efficient and commonly used in loops
  • Convert to list() if you want to view or print the sequence

Practice Problem

Print all even numbers between 10 and 30 using range().

for i in range(10, 31, 2):
    print(i)
10
12
14
16
18
20
22
24
26
28
30

What to remember

Connect what you noticed in the example with the idea below.

Remember

range() represents an arithmetic sequence of integers. Its stop value is excluded, and it does not store every integer as a list.

Watch out for this idea

range(n) includes the stop value n.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →