np.arange()
np.arange() counts from a start to a stop in equal steps, just like Python's range but producing an array.
In this page:
Syntax
np.arange(start, stop, step)
np.arange()
np.arange(start, stop, step) returns evenly spaced values in the half-open interval [start, stop). Unlike range it accepts float steps. With floats, rounding can make the length surprising, so prefer linspace there.
Note:
With one argument, arange(5) starts at 0 and stops before 5.
Example: np.arange()
import numpy as np
print(np.arange(5))
print(np.arange(2, 10, 2))
print(np.arange(0, 1, 0.25))
# Output:
# [0 1 2 3 4]
# [2 4 6 8]
# [0. 0.25 0.5 0.75]
Related Topics
Common Mistakes
- Expecting the stop value to be included
- Using float steps and getting an unexpected length
- Confusing arange with linspace
Chapter Summary
- arange(start, stop, step) excludes stop
- Works with float steps
- Returns an ndarray
- Use linspace when you need an exact count
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: