Python - numpy.arange() With Examples

Being a linear sequence generator, the numpy.arange() function is used to generate a sequence of numbers in linear space with a uniform step size.

This is similar to another function, numpy.linspace(), which also generates a linear sequence with a uniform step size.

Let’s understand how we can use this function to generate different sequences.




  • start -> The starting point (included) of the range, which is set to 0 by default.
  • stop -> The ending point (excluded) of the range
  • step -> The step size of the sequence, which is set to 1 by default. This can be any real number except Zero.
  • dtype -> The type of the output array. If dtype is not given (or provided as None), the datatype will be inferred from the type of other input arguments.

Let us take a simple example to understand this:

This will generate a linear sequence from 0.2 (included) until 2 (excluded) with a step size of 0.1, so there will be (2 – 0.2)/0.1 – 1 = 20 elements in the sequence, which is the length of the resulting numpy array.


Here is another line of code which generates the numbers from 0 to 9 using arange(), using the default step size of 1:

If the step size is provided as 0, this is not a valid sequence, since a step of 0 implies you are dividing the range by 0, which will raise a ZeroDivisionError Exception.


NOTE: This function is a bit different from numpy.linspace(), which, by default, includes both the starting and the endpoints for the sequence calculation. It also does not take the step size as an argument, but rather takes only the number of elements in the sequence.

A simple example

Let’s now put all of this together into a simple example to demonstrate the linearity of the sequences generated by numpy.arange().

The following code plots 2 linear sequences between [0, 20] and [0, 10] using numpy.arange() to show that there is uniformity generated by the sequence, so the resulting arrays are linear.


Python - numpy.arange()

Numpy Arange

As you can see, the orange dots represent a linear sequence from 0 to 10 having a step size of 2 units, but since 10 is not included, the sequence is [0, 2, 4, 6, 8]. Similarly, the blue dots represent the sequence [0, 4, 8, 12, 16].

numpy.arange() vs range()

The whole point of using the numpy module is to ensure that the operations that we perform are done as quickly as possible, since numpy is a Python interface to lower level C++ code.

Many operations in numpy are vectorized, meaning that operations occur in parallel when numpy is used to perform any mathematical operation. Due to this, for large arrays and sequences, numpy produces the best performance.

Therefore, the numpy.arange() is much faster than Python’s native range() function for generating similar linear sequences.

Performance Test

We should not interleave numpy‘s vectorized operation along with a Python loop. This slows down performance drastically, as the code is iterating using native Python.

For example, the below snippet shows how you should NOT use numpy.

The recommended way is to directly use the numpy operation.

Let’s test the difference in performance using Python’s timeit module.


As you can see, numpy.arange() works particularly well for large sequences. It’s almost 20 times (!!) as fast as the normal Python code for a size of just 1000000, which will only scale better for larger arrays.

Therefore, numpy.arange() should be the unanimous choice among programmers when working with larger arrays.

For smaller arrays, when the difference in performance isn’t that much, you could use among either of the two methods.


By admin

Leave a Reply