# Use the Python range() Function to Generate Sequences of Numbers

Tyler (291)
Total time: 5 minutes

In Python, `range` is an immutable sequence type, meaning it's a class that generates a sequence of numbers that cannot be modified. The main advantage to the `range` class over other data types is that it is memory efficient. No matter how large a sequence you want to iterate over, the `range` class only stores the `start`, `stop`, and `step` values (we'll cover these later), not the entire sequence of numbers.

In this guide we'll cover various was to use the `range` class.

Posted in these interests:
h/python67 guides

## Basic usage

While it looks more like a built-in function, `range` is actually a class. So when you use range, you're actually passing arguments into the `range` constructor.

When given a single argument, `range` will use this value as the stop value. Stop refers to the end of the sequence. Keep in mind that `range` sequences are not inclusive, meaning the sequence will contain numbers up to but not including the stop value.

``range(stop)``

Example:

``list(range(10))``

Output:

``[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]``

Note: In order to see the numbers in the sequence, we must convert the sequence to a list.

Notice how `range` assumes a start value of `0` and that the sequence contains numbers up to but not including the stop value.

## Specifying start and stop values

As one might guess, you can also specify the start value. When two arguments are passed to the `range` constructor, the first is start and the second is stop.

``range(start, stop)``

Example:

``list(range(10, 20))``

Output:

``[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]``

## Incrementing by something other than one

The default step value is `1`, but sometimes we'll want to increment by something other than 1. If provided a third argument, it will be used as the step value.

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

Example:

``list(range(10, 20, 2))``

Output:

``[10, 12, 14, 16, 18]``

## Iterating over a range

Iterating over a range of numbers is easy using the Python for loop.

Example:

``````for x in range(5):
print(x)``````

Output:

``````0
1
2
3
4``````

## Iterating a specific number of times

You can use the `range` function to iterate a specific number of times, even when you don't need to access each element of the sequence.

Imagine we want to build a string that contains ten of a certain character.

Example:

``````result = ''

for x in range(10):
result += '#'

print(result)``````

Output:

``##########``

In this example, `range` provides a memory efficient way of iterating exactly ten times.

## Reversing a range

Python provides a built-in function for reversing sequences called `reversed`.

Example:

``list(reversed(range(10)))``

Output:

``[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]``

## Using slice notation on a range

You can access indexes of a range as well as use slice notation to get a subsection of the sequence.

Example:

``list(range(100)[10:20])``

Output:

``[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]``

## Conclusion

At this point, you should have a solid understanding of what the `range` class is and how it's used. Do you have any questions or feedback? Let me know in the comments below.

Tyler
Joined in 2015
Software Engineer and creator of howchoo.
Related to this guide:
Get the latest edition of Python in just minutes.
In these interests: codewindowspython
Run Python scripts in command prompt without typing the whole path.
In these interests: windowscodepython
You can run any Python script in a command-line interface.
In these interests: python
Got a Python question? We've probably answered it here.
Not sure what version of Python you’re running? Time to find out!
Specify a parallel filesystem cache for compiled bytecode
AKA the Walrus Operator
Learn how to use formatted string literals in Python
Posted in these interests:
h/python 67 guides
Python is howchoo's favorite programming language. We believe python promotes the most organized and performant codebase possible. We also love Django so, naturally, we love Python.
Discuss this guide:
We're hiring!
Are you a passionate writer? We want to hear from you!

Follow @howchoo and learn cool things:

Like what we do?

Want to support Howchoo? When you buy a tool or material through one of our Amazon links, we earn a small commission as an Amazon Associate.