Numpy.arange() Method

Numpy.arange() method returns an array with evenly spaced elements as per the interval. The interval mentioned is half-opened, i.e., [Start, Stop).

Syntax

numpy.arange(start, stop, step, dtype)

Parameters

  1. start (optional, number): Start of an interval. The default start value is 0.
  2. stop: number. End of the interval.
  3. step (number, optional): The step can’t be zero. Otherwise, you’ll get a ZeroDivisionError.
  4. dtype: The type of an output array. If the dtype is not given, infer the data type from the other input arguments.

Return Value

It returns an array.

Example 1: Basic usage of np.arange()

Visual representation of np.arange() method

import numpy as np

print(np.arange(4), "\n")

Output

[0 1 2 3]

Example 2: Specifying start and stop

Passing start and stop argument to the arange()

import numpy as np

arr = np.arange(18, 22)

print(arr)

Output

[18 19 20 21]

Example 3: Specifying step size

Specifying step size

import numpy as np

arr = np.arange(17, 22, 2)

print(arr)

Output

[17 19 21]

Example 4: Passing ‘float’ arguments

Passing float value to np.arange()

import numpy as np

arr = np.arange(2, 11.1, 3)

print(arr)

Output

[ 2. 5. 8. 11.]

Example 5: Creating a multi-dimensional array

Creating a multi-dimensional array

import numpy as np

print(np.arange(4).reshape(2, 2), "\n")

Output

[[0 1]
 [2 3]]

That’s it.

Related posts

numpy.select()

numpy.argwhere()

numpy.insert()

numpy.append()

numpy.transpose()

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.