What is the arange() function from NumPy in Python?

Overview

The arange() function in Python returns evenly spaced values within a given interval from NumPy.

Syntax

arange([start, ]stop, [step, ]dtype=None, *, like=None)

Parameter values

The arange() function takes the following parameter values:

  • start: this represents the start of the interval. The default value for start is 0. This is optional.
  • stop: this represents the end of the interval. The interval does not include this value.
  • step: this represents the spaces between the values. This is optional.
  • dtype: this represents the data type of the output array.
  • like: this represents the array-like object or prototype.

Return value

The arange() function returns an array of evenly spaced values.

Code example

from numpy import arange
# creating the array
my_array = arange(1, 11, 1, dtype = int)
print(my_array)

Code explanation

  • Line 1: We import the arange function from numpy library.
  • Line 4: Using the arange() function, we create an array to start at 1, stop at 11 (11 not included), with a step size of 1, and with an int data type. The result is assigned to the variable my_array.
  • Line 5: We print the array my_array.

Free Resources