BetterDocs
Home
Docs

Creation | np.arange()

Method:

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

Creates a NumPy array of values within a specified range.

Returns:

np.ndarray

Parameters:

start: int, Optional-

Starting value of the NumPy array (optional; defaults to 0).

import numpy as np
# Range from 0 (default) to 10 with step 1 (default)
range_array = np.arange(stop=10)
# Including 'start' and excluding 'stop'
print(range_array)
'''
Output:
[0 1 2 3 4 5 6 7 8 9]
'''

If start is not specified, step is automatically understood as 1.

start can have negative values.

stop: int-

End of the interval value of the NumPy array.

import numpy as np
range_array = np.arange(start=1, stop=10, step=2)
# Including 'start' and excluding 'stop'
print(range_array)
'''
Output:
[1 3 5 7 9]
'''

If stop >= start an empty array is returned.

step: int, Optional-

End of the interval value of the NumPy array.

import numpy as np
range_array = np.arange(start=-5, stop=0)
# Including 'start' and excluding 'stop'
print(range_array)
'''
Output:
[-5 -4 -3 -2 -1]
'''

dtype: None, Optional-

The desired data-type for the NumPy array is inferred from the start & stop range.

import numpy as np
range_array = np.arange(start=-5, stop=0, dtype=np.float64)
# Including 'start' and excluding 'stop'
print(range_array)
print(range_array.dtype)
'''
Output:
[-5. -4. -3. -2. -1.]
float64
'''

Values: +

By passing the dtype, you are implicitly converting the values to the passed dtype.

device: None, Optional-

In NumPy 2.1, the device parameter in np.arange() is still in an early stage and currently only accepts "cpu" as a valid option. At this time, it is primarily there for future development, allowing the function to eventually support GPUs or other devices, but for now, it doesn't support GPU IDs directly.

like: None, Optional-

The like parameter in np.arange() was introduced to make array creation more flexible, particularly for compatibility with libraries that extend NumPy, like Dask, CuPy, or other libraries that create arrays compatible with np.ndarray but optimized for different backends (e.g., parallel processing or GPU-based arrays).


Logo

BetterDocs

Support

EmailDiscordForms

Documentations

Python

Company

AboutDocs

Policies

Terms of ServicePrivacy Policy