• author: Python and Pandas with Reuven Lerner

The many uses of Range in Python

If you are starting your journey as a Python programmer and exploring the language’s built-in functions, it won't be long before you come across Range(). Range() is a built-in function that returns an immutable sequence of numbers and is used for looping in Python.

History of Range()

In the early days of Python, to loop through a sequence multiple times, you had to manually create a list of integers. Range had an optional argument, unlike range, which would return an iterator instead of a list. But in Python 3, range() doesn't produce a list anymore. Instead, it produces a range object that doesn't allocate a memory for the full sequence of elements.

Creating A Range

The easiest and most commonly used way to create a range is by using the inclusive and exclusive arguments. range(5) would make an iterator containing [0, 1, 2, 3, 4].

Let us take an example, R = range(5, 28), which means from 5 till and not including 28. By iteration, we will receive all the numbers from five till 28, not including 28. If you pass a step size of 15, you'll get incremental values of 15. R = range(5,50,15) would create [5, 20, 35].

What if we need to create a sequence that counts backward from 10 to 1? To get a range of descending numbers, we can set the step argument to be minus one. R = range(10, 1, -1) would create the sequence of [10, 9, 8, 7, 6, 5, 4, 3, 2].

Attributes of Range

The range is an immutable built-in function in Python that has three primary attributes: start, stop, and step. We can retrieve these individual components of a range object by calling .start, .stop, and .step, respectively. Below is an example of how to extract the range values by calling these built-in methods:

r = range(100, 1500, 123)
print(f'"start:" {r.start}, "stop:" {r.stop}, "step:" {r.step}')

# Output:
# start: 100, stop: 1500, step: 123

One fascinating feature of the range object in Python is that regardless of how long it is, it requires very little memory. The memory usage of the built-in range function is constant, as the range object does not create a list with each element; instead, it calculates the next value of the range object on demand.

Range Testing

We can quickly evaluate the presence of a specific number in a range. To determine if a number, say 223, exists in a range, call it by using the in operator.

r = range(1000, 3000, 10)
print(223 in r)

# Output: 
# True

What if we need to extract a range's specific range—let's say numbers between 4 and 10? We can define a new range with our desired values by calling range() with the relevant start and stop values.

r = range(1000, 3000, 10)
new_r = range(4, 10)

# Output:
[1040, 1050, 1060, 1070, 1080, 1090]

Ranges can be incredibly versatile, and we can also turn them into lists, compare and iterate through them, and use them for indexing.

It is always beneficial to familiarize yourself with Python's built-in functions and master their features and functions to write efficient code. And after exploring everything this article offers, you should have a more comprehensive understanding of how to use Range() to generate iterative sequences in Python.

So the next time you want to loop through a sequence multiple times, use Range() and let the whole process run efficiently.

Previous Post

The Quick and Easy Way to Install GPT Engineer

Next Post

Understanding Bytes in Python: Exploring Built-in Type in Standard Library

About The auther

New Posts

Popular Post