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

Tyler (275)
0

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:

python
PRIMARY
62 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:
Python is a very popular programming language for data visualization.
Nick
In these interests: python
In Python, comprehensions are a useful construct that allows us to create new sequences in a very concise way.
Tyler
In these interests: codepython
Slack has become one of the most important tools for communication in many companies, mine included.
Tyler
In these interests: slackpython
If you're familiar with Python's keyword-only arguments, then you've probably wondered why the same constraint doesn't exist for positional arguments. This changes with Python 3.
Specify a parallel filesystem cache for compiled bytecode
AKA the Walrus Operator
Learn how to use formatted string literals in Python
While this requirement may seem rare, learning how to nest defaultdicts properly can be extremely powerful and save you from bloated and confusing initialization code.
In Python, we often run into scenarios where we need to merge two or more lists. This is also called list concatenation, and Python provides multiple methods for accomplishing this.
Find and fix slow running Python unit tests
Posted in these interests:
python
PRIMARY
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 or editor? 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.