In this article, we will take a look at range vs arange in Python. Learning the difference between these functions will help us understand when to either of them in our programs. Both range and arange functions of Python have their own set of advantages and disadvantages. This article will help us learn about them in detail.
To better understand the difference between range vs arange functions in Python, let us first understand what each of these functions’ do.
Table of Contents
range vs arange in Python: Understanding range function
The range function in Python is a function that lets us generate a sequence of integer values lying between a certain range. The function also lets us generate these values with specific step value as well . It is represented by the equation:
range([start], stop[, step])
So, in the above representation of the range function, we get a sequence of numbers lying between optional start & stop values. Next, each of these values are also getting incremented by the optional step values.
range function example 1
So that was the theory behind the range function. Now, let understand it better by practicing using it. Fire up your Python IDLE interpreter and enter this code:
l = range(1, 10, 2)
When you hit enter on your keyboard, you don’t see anything right? That is because the resulting sequence of values are stored in the list variable “l”.
To be able to see the values stored in it, let us print individual list values. So, by indexing each of the list items, we get the following values printed out.
>>> l 1 >>> l 3 >>> l 5 >>> l 7 >>> l 9
So we see a list of numbers, cool! But when you observe closely, you will realize that this function has generated the numbers in a particular pattern. You can see that the first number it has generated is after taking into consideration our optional start parameter. We had set its value to 1. Next, it is also honoring the stop value by printing the value 9, which is within our defined stop value of 10. If you try to index the list for any further value beyond this point will only return an error:
>>> l Traceback (most recent call last): File "<pyshell#7>", line 1, in <module> l IndexError: range object index out of range
So, this confirms that the last value we get will always be less than the stop value.
But the most important thing to observation we need to make here is the step size between each of these values. We can see that each of these values are incremented exactly with a step size of 2. This is the same step size we had defined in our call to range function (the last parameter)!
range function example 2
Does this mean everytime we want to call range function, we need to define these 3 parameters? Not really. If we take a look at the signature of the arange function again:
range([start], stop[, step])
The parameters start and step (mentioned within the square brackets) are optional. This means that we can call the range function without these values as well like this:
nl = range(4) >>> nl range(0, 4) >>> nl 0 >>> nl 1 >>> nl 2 >>> nl 3 >>> nl
In this case, we have only the stop value of 4. As a result we get our sequence of integers starting with a default value of 0. The step value is also defaulted to a value of 1. So we get the integers in the range between 0 to 3, with a step value of 1.
Alright then, hope everything is clear to you up to this point. If this is the case with Python’s range function, what does the arange function in Python do?
range vs arange in Python: Understanding arange function
Unlike range function, arange function in Python is not a built in function. But instead, it is a function we can find in the Numpy module. So, in order for you to use the arange function, you will need to install Numpy package first!
The signature of the Python Numpy’s arange function is as shown below:
numpy.arange([start, ]stop, [step, ]dtype=None)
Wait a second! Doesn’t this signature look exactly like our range function? Yes, you are right! Python’s arange function is exactly like a range function. It also has an optional start and step parameters and the stop parameter.
But then what is the difference between the two then?
range vs arange in Python – What is the difference?
Where the arange function differs from Python’s range function is in the type of values it can generate.
The built in range function can generate only integer values that can be accessed as list elements. But on the other hand, arange function can generate values that are stored in Numpy arrays. We can observer this in the following code:
import numpy as np a = np.arange(4) >>> a array([0, 1, 2, 3]) >>> a 0
We are clearly seeing here that the resulting values are stored in a Numpy array. Each of the individual values can hence also be accessed by simply indexing the array!
range vs arange in Python – Advantages & Disadvantages
This begs us the next question. When should we use Python’s built-in range function vs Numpy’s arange function? To understand this, lets list out the advantages and disadvantages of each of these functions:
Advantages of range function in Python
- range function returns a list of integers in Python 2. In case of Python 3, it returns a special “range object” just like a generator.
Disadvantages of range function in Python
- range function is considerably slower
- It also occupies more memory space when handling large sized data objects.
Advantages of arange function in Python
- Numpy’s arange function returns a Numpy array
- Its performance is wat better than the built-in range function
- When dealing with large datasets, arange function needs much lesser memory than the built-in range function.
So this is the fundamental difference between range vs arange in Python. We can understand them even better by using them more in our everyday programming.
I hope this gave you some amount of clarity on the subject. If you still have any more doubts, do let me know about it in the comments below. I will be more than happy to help you out.
Having said that, take a look at this article. It gives you a simple explanation on the “Difference between expressions and statements in Python“. I have spent considerable amount of time trying to understand these topics. Since there are not many articles available that explains them clearly, I started this blog to capture these topics. Hope you found them useful! If yes, do share them with your friends so that it can help them as well.
With this, I will conclude this article right here. See you again in my next article, until then, ciao!