Home Interests Python

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

howchoo
howchoo   (467)
September 12, 2023
7 minutes

Share

Interests
Posted in these interests:
python • 18 guides

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 startstop, 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.

1 – 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.

2 – 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]

3 – 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]

4 – Iterating over a range

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

A Guide to Python “For” Loops
A comprehensive guide on Python “for” loops

Example:

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

Output:

0
1
2
3
4

5 – 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.

6 – 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]

7 – 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]

8 – 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.

NEXT UP

Class Vs. Instance Variables in Python 3

howchoo
howchoo   (467)
November 22, 2023

When learning object oriented programming in Python, there can be a few gotchas when it comes to distinguishing between class and instance variables. In this guide I’ll explain the difference between class and instance variables and provide examples demonstrating various use cases. 1 – Class vs. instance variables First, a quick review if you’re new

Continue Reading

howchoo

 467 guides

Introducing Howchoo, an enigmatic author whose unique pen name reflects their boundless curiosity and limitless creativity. Mysterious and multifaceted, Howchoo has emerged as a captivating storyteller, leaving readers mesmerized by the uncharted realms they craft with their words. With an insatiable appetite for knowledge and a love for exploration, Howchoo's writing transcends conventional genres, blurring the lines between fantasy, science fiction, and the surreal. Their narratives are a kaleidoscope of ideas, weaving together intricate plots, unforgettable characters, and thought-provoking themes that challenge the boundaries of imagination.

Discover interesting things!

Explore Howchoo's most popular interests.

Explore