Skip to main content

How to Make a Sequence of Numbers in Python with range

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

When you need the numbers 0 to 4, or 1 to 5, or every third number up to 10, Python's range makes them for you. It's what for loops usually count with. This guide makes three sequences and turns each into a list to show it, with real output from Python 3.15.2.

Syntax​

Syntax
range(<stop>)
range(<start>, <stop>)
range(<start>, <stop>, <step>)
PartWhat it means
<start>The first number. Without it, 0
<stop>Where the sequence ends. This number itself is not included
<step>The gap between numbers. Without it, 1; a negative step counts down

The built-in types reference: "The range type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in for loops."

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

list(...) turns each range into a list, so print shows every number in it.

count.py
print(list(range(5)))
print(list(range(1, 6)))
print(list(range(0, 10, 3)))

The count.py script in a macOS-style code window: print(list(range(5))), print(list(range(1, 6))) and print(list(range(0, 10, 3))).

Step 2: Run It​

Terminal
uv run python count.py
Output
[0, 1, 2, 3, 4]
[1, 2, 3, 4, 5]
[0, 3, 6, 9]

The script's output in a terminal: [0, 1, 2, 3, 4], [1, 2, 3, 4, 5] and [0, 3, 6, 9].

range(5) stops before 5, so to reach 5 the second call ends at 6. The step of 3 gave 0, 3, 6, 9.

Common Mistakes​

MistakeWhat happensDo this instead
Expecting the stop number in the resultrange(1, 5) ends at 4Use one more than the last number you want
Printing a range directlyprint(range(5)) printed range(0, 5), not the numberslist(range(5)), or loop over it with for

Troubleshooting​

SymptomLikely causeFix
TypeError: 'float' object cannot be interpreted as an integerA decimal number such as range(0.5)range takes whole numbers only; use int(...)

FAQ​

Can range count down?​

Yes, with a negative step: list(range(5, 0, -1)) gave [5, 4, 3, 2, 1].

Does range make all the numbers at once?​

No. The reference says a range "only stores the start, stop and step values, calculating individual items and subranges as needed", which is why print shows range(0, 5) instead of a list.

Conclusion​

range(5), range(1, 6) and range(0, 10, 3) gave [0, 1, 2, 3, 4], [1, 2, 3, 4, 5] and [0, 3, 6, 9]: start at the first number, step by the step, and stop before the stop.

Additional Resources​