NumPy arange(): Create Arrays with Regularly Spaced Values

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().

ParameterDescription
startNumber or array-like, optional (default is 0). Start of interval (inclusive)
stopNumber or array-like. End of interval (exclusive)
stepNumber, optional (default is 1). Spacing between values
dtypeData-type of the output array; if not given, inferred from input arguments

Syntax and Parameters

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

Basic arange Examples

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]))

Using Start, Stop and Step

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

Negative Intervals and Steps

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

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]

Specifying dtype

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

Practice in Google Colab

Test arange() intervals, negative steps, float sequences, and run self-verifying test suites in our interactive Google Colab companion notebook:

Interactive Practice: Colab & GitHub

Launch the notebook directly in Google Colab to run live code cells or inspect the source repository on GitHub:

View on GitHub Open In Colab

Continue exploring related array creation functions: array(), zeros(), ones(), full(), eye(), and linspace().




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer