In this article we will discuss how to create a Numpy array of evenly spaced numbers over a given interval using numpy.arrange().

numpy.arrange()

Python’s numpy module provides a function to create an Numpy Array of evenly space elements within a given interval i.e.

Arguments:
start : It’s the start value of range. It’s optional, if not provided default value be 0.
stop : End Value of range, array doesn’t include this value but it’s an end marker
step : Spacing between two adjacent values. It’s optional, if not provided default value be 1.
dtype : Data type of elements, if not provided will be deduced from other arguments.

This function returns an evenly spaced array of numbers from range start to stop -1 with equal intervals of step.

To use Numpy in our code we need to import following module i.e.

Checkout some examples,

Example 1:

Create a Numpy Array containing numbers from 5 to 30 but at equal interval of 2

Here, start of Interval is 5, Stop is 30 and Step is 2 i.e.

It will return a Numpy array with following contents,

Example 2:

Create a Numpy Array containing elements from 1 to 10 with default interval i.e. 1

As step argument is option, so when it is not provided then it’s default value will be 1. Let’s create a Numpy array from where start of interval  is 5, Stop of interval is 30 and step size is default i.e 1 ,

It will return a Numpy array with following contents,

Example 3:

Create a Numpy Array containing elements up to 20 with default start and step size

As start & step arguments are optional, so when we don’t provide these arguments then there default value will be 0 & 1.
Let’s create a Numpy array with default start & step arguments stop of interval is 20 i.e.

It will return a Numpy array with following contents,

Complete example is as follows,

Output:

 

 

Join LinkedIn Group of Python Professional Developers who wish to expand their network and share ideas.

You can also follow us On Twitter :

Click Here to Subscribe for more Articles / Tutorials like this.