Register Login

range() vs. arange() in Python

Updated Jan 16, 2022

Working with a lot of numbers and generating a large range of numbers is always a common task for most Python programmers. For generating a large collection of contiguous numbers, Python has different types of built-in functions under different libraries & frameworks. In this article, you will learn the difference between two of such range-based number generating functions.

The range() function:

Python's range() function is a built-in function of the standard Python interpreter that helps programmers spawn a series of integer values prevailing between a specific range. So, the range() function will accept three parameters: start, stop, and step. The start value defines the starting point from where the function will start spawning the numbers. The stop value defines the number-1 up to which the range function will generate the number. Finally, the step function will define the number of jumps or the number of gaps the range function will make in between the start and the stop values.

Syntax:

range(start, stop, step)

Example:

for x in range(1, 11, 2):
   	print(x)

Output:

1
3
5
7
9

Apart from this, there are three different forms of using the range(). Programmers can use the range() with a single parameter. By default, this parameter will be the stop value that will define up to how much the range will generate the consecutive values. By default, the stop value considers by subtracting one from the actual value. So, if you choose stop parameter as 10, the count will start from 0 up to 9.

Example:

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

Output:

0
1
2
3
4
5
6
7
8
9
10

Again, when programmers provide two parameters to the range() function, Python's range() function considers them as the start and stop values.

Example:

for x in range(1, 11,):
    print(x)

Output:

1
2
3
4
5
6
7
8
9
10

The third way of calling the range() function is when programmers pass three parameters separated by two commas. The first parameter defines the start, the second defines the stop, and the third defines the step.

Example:

for x in range(1, 11, 2):
    print(x)

Output:

1
3
5
7
9

The arange() function:

NumPy is a popular Python library that deals explicitly with the numeric aspects of programming. It has the most essential data type that is the ndarray. It is similar to that of a normal array found in other programming languages like C, C++, Java, etc. The ndarray stores homogenous data.

The ndarray uses a built-in NumPy library function called the arange() that creates numerical ranges. Programmers create the arange() or most popularly called numpy.arange() when dealing with data science-related libraries like SciPy, Matplotlib, Pandas, etc.

NumPy arrays are fast and creating a homogenous array using the arange() makes the entire program more efficient. This function creates an instance of the ndarray with evenly gapped values & returns a reference to it.

Syntax:

numpy.arange([start, stop, step], dtype=None)

Exxample:

import numpy as np
x = np.arange(1, 10, 3)
print(x)

Output:

[1 4 7]

It also has three different ways of calling or using it.

np.arange(stop) when it takes one argument.

import numpy as np
x = np.arange(10)
print(x)

Output:

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

np.arange(start,  stop) when it takes two arguments.

import numpy as np
x = np.arange(1, 10)
print(x)

Output:

[1 2 3 4 5 6 7 8 9]

np.arange(start,  stop,  step) when it takes three arguments.

import numpy as np
x = np.arange(1, 11, 2)
print(x)

Output:

[1 3 5 7 9]

Let us now check the difference between range() and arange().

range() vs. arange():

Although, both of them do the same type of consecutive number generation, there exist certain differences between both of them. Let us now address the distinction between them:

range() arange()
It comes as a default function of Python. It comes under the NumPy library of Python.
It is independent of any library or module. It depends on the NumPy library. Without importing the NumPy, you will not be able to use it.
It generates a simple series of numbers based on the given range. It generates a series of numbers but in the form of a ndarray (NumPy array).
range() returns a successive collection of numbers which are individual values or range object. It returns a NumPy array or ndarray.
Since, each of the numbers are individual and independent, generating them is faster. Even though it generates homogenous values, the array binds them together as a collection object of Python.
It is independent of any module and hence more efficient. Since, arange() works with ndarray, the programmers needs to import the library and hence it is slower.
It is not a good option when the programmer has to deal with a collection of values packed as a single unit. It is a good option when the programmer has to deal with a collection object as the function returns an array.
For using the range(), you do not have to install any module. For using the arange(), you have to install the NumPy module.
This comes as a built-in default function with the Python interpreter. This comes as a third-party module-based function.
Programmers use it mostly with for or while to count the iteration. You cannot use the range() directly with the print. Programmers use it mostly to deal with large data sets or collection of data that resides within the array (NumPy array).
range() is fast and efficient if iterated over the same sequence multiple times. arange() is fast and efficient if used with the NumPy data structure.

Conclusion:

When dealing with large data sets, range() function will be less efficient as compared to arange(). This is because the arange() takes much lesser memory than that of the built-in range() function. The range function is considerably slower as it generates a range object just like a generator. It takes more memory space when dealing with large sized Python objects. But both have their own benefits at different situations.


×