← Back to NumPy Course | Chapter 2: Creating Arrays | Lesson 3 of 7

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:

  1. np.arange()
Syntax
python
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()

python
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
  1. Expecting the stop value to be included
  2. Using float steps and getting an unexpected length
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.