numpy.arange([start, ] stop, [step, ] dtype=None, *, like=None) returns an ndarray with evenly spaced values within a given interval. Values are generated within the half-open interval [start, stop).The arange() function is similar to Python's built-in range() function, but returns a NumPy ndarray rather than a list. It is widely used for generating time axes, sequence indices, and coordinate ranges. For linearly spaced arrays with a fixed number of points, see linspace().
| Parameter | Description |
|---|---|
start | Number or array-like, optional (default is 0). Start of interval (inclusive) |
stop | Number or array-like. End of interval (exclusive) |
step | Number, optional (default is 1). Spacing between values |
dtype | Data-type of the output array; if not given, inferred from input arguments |
numpy.arange(start, stop, step, dtype=None)
Passing a single argument sets the upper limit stop, starting at 0 with a step of 1:
import numpy as np
print(np.arange(0)) # []
print(np.arange(5)) # [0 1 2 3 4]
assert np.array_equal(np.arange(5), np.array([0, 1, 2, 3, 4]))
Specify explicit start, stop, and step values:
print(np.arange(start=1, stop=5, step=2)) # [1 3]
print(np.arange(1, 5, 2)) # [1 3]
# Creating an empty array when start >= stop
x = np.arange(3, 3)
print(x) # []
assert x.size == 0
When counting downwards, a negative step value is required:
print(np.arange(-10, -3, 2)) # [-10 -8 -6 -4]
print(np.arange(-3, -10, -2)) # [-3 -5 -7 -9]
print(np.arange(-3, 10, 3)) # [-3 0 3 6 9]
Floating-point steps are supported, though accumulation rounding can occasionally produce unexpected endpoints due to precision limits:
print(np.arange(0.4, 2.8, 0.7)) # [0.4 1.1 1.8 2.5]
print(np.arange(-2.8, 2, 0.9)) # [-2.8 -1.9 -1. -0.1 0.8 1.7]
Enforce exact integer or floating-point data types with the dtype argument:
print(np.arange(1, 5, dtype=np.int8)) # [1 2 3 4]
print(np.arange(1, 5, dtype=np.float64)) # [1. 2. 3. 4.]
my_ar = np.arange(1, 5, dtype=np.float64)
print("Array dtype:", my_ar.dtype) # float64
assert my_ar.dtype == np.float64
Test arange() intervals, negative steps, float sequences, and run self-verifying test suites in our interactive Google Colab companion notebook:
Launch the notebook directly in Google Colab to run live code cells or inspect the source repository on GitHub:
View on GitHubContinue exploring related array creation functions: array(), zeros(), ones(), full(), eye(), and linspace().
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.