arange() (NumPy)
np.arange() creates a 1-D array of evenly spaced values within a range, like Python's range() but returning a NumPy array and supporting a float step. It is the standard way to generate a sequence of numbers.
NumPy
arange(start, stop, step) produces values from start up to but not including stop, advancing by step. With one argument it counts from 0; with two it counts from start to stop. Unlike range, it accepts non-integer steps such as 0.5, which is handy for sampling.
Because the stop is exclusive and floats can't be represented exactly, using a fractional step can occasionally include or exclude an unexpected final value. When you need a precise number of points across an interval, np.linspace is more reliable.
arange is the fastest way to make an index array, a sequence to reshape into a grid, or the x-values for a plot.
Syntax
np.arange(start, stop, step, dtype=None)Examples
Python
import numpy as np
print(np.arange(0, 10, 2))Output:
[0 2 4 6 8]