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.

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

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.

import numpy as np

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.

# Start = 5, Stop = 30, Step Size = 2
arr = np.arange(5, 30, 2)

It will return a Numpy array with following contents,
[ 5  7  9 11 13 15 17 19 21 23 25 27 29]

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 ,

# Start = 1, Stop = 10. As Step Size is not provided, so default value be 1
arr = np.arange(1, 10)

It will return a Numpy array with following contents,
[1 2 3 4 5 6 7 8 9]

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.

# Stop = 20. As Start & Step Size is not provided, so default value be 0 & 1 respectively
arr = np.arange(20)

It will return a Numpy array with following contents,
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]

Complete example is as follows,
import numpy as np

def main():
   print('*** Create numpy array using numpy.arange() ***')

   print('Create a Numpy Array containing elements from 5 to 30 but at equal interval of 2')
   # Start = 5, Stop = 30, Step Size = 2
   arr = np.arange(5, 30, 2)

   print('Contents of the Array : ', arr)

   print('Create a Numpy Array containing elements from 1 to 10 with default interval i.e. 1')
   # Start = 1, Stop = 10. As Step Size is not provided, so default value be 1
   arr = np.arange(1, 10)

   print('Contents of the Array : ', arr)

   print('Create a Numpy Array containing elements up to 10 with default start and default step size')
   # Stop = 20. As Start & Step Size is not provided, so default value be 0 & 1 respectively
   arr = np.arange(20)

   print('Contents of the Array : ', arr)

if __name__ == '__main__':
   main()


Output:
*** Create numpy array using numpy.arange() ***
Create a Numpy Array containing elements from 5 to 30 but at equal interval of 2
Contents of the Array :  [ 5  7  9 11 13 15 17 19 21 23 25 27 29]
Create a Numpy Array containing elements from 1 to 10 with default interval i.e. 1
Contents of the Array :  [1 2 3 4 5 6 7 8 9]
Create a Numpy Array containing elements up to 10 with default start and default step size
Contents of the Array :  [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]

 

 

Join a list of 2000+ Programmers for latest Tips & Tutorials