🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.arange()

AI & DATA SCIENCE // np-arange

np.arange() creates a 1D array containing evenly spaced values over a given interval, similar to Python's built-in range() but returning an actual array and supporting float steps.

Syntax

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

Deep Dive Course

np.arange(stop) generates values from 0 up to, but not including, stop; np.arange(start, stop, step) generalizes this with an explicit start and step size, which, unlike range(), can be a float, letting you generate fractional sequences. Because of floating-point rounding error, the exact number of elements produced by a float step can sometimes be off by one from what you'd expect mathematically, which is one reason np.linspace() is often preferred when you need a precise, known number of points.

1Understanding np.arange()

np.arange(stop) generates values from 0 up to, but not including, stop; np.arange(start, stop, step) generalizes this with an explicit start and step size, which, unlike range(), can be a float, letting you generate fractional sequences. Because of floating-point rounding error, the exact number of elements produced by a float step can sometimes be off by one from what you'd expect mathematically, which is one reason np.linspace() is often preferred when you need a precise, known number of points.

💡

For float ranges where you need an exact, known number of points rather than a step size, use np.linspace(start, stop, num) instead of np.arange() — floating-point rounding can make arange's element count slightly unpredictable.

editor.html
import numpy as np

arr = np.arange(0, 10, 2)
print(arr)
localhost:3000

2Practical Example

Here is a real-world application of np.arange() showing how it is used in production NumPy code.

editor.html
import numpy as np

arr = np.arange(0, 1, 0.25)
print(arr)
localhost:3000

3Best Practices

Follow these guidelines when working with np.arange():

1. Use np.arange() for integer sequences or when you specifically know the step size you want

2. Use np.linspace() instead of np.arange() when you need an exact number of evenly spaced float values, to avoid floating-point step-count surprises

3. Specify an explicit dtype when the default type inference isn't what you want

⚠️

Tip: For float ranges where you need an exact, known number of points rather than a step size, use np.linspace(start, stop, num) instead of np.arange() — floating-point rounding can make arange's element count slightly unpredictable.

editor.html
import numpy as np

arr = np.arange(0, 10, 2)
print(arr)
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.arange(0, 10, 2)
print(arr)
Example 02Advanced Example
import numpy as np

arr = np.arange(0, 1, 0.25)
print(arr)

Best Practices

  • Use np.arange() for integer sequences or when you specifically know the step size you want
  • Use np.linspace() instead of np.arange() when you need an exact number of evenly spaced float values, to avoid floating-point step-count surprises
  • Specify an explicit dtype when the default type inference isn't what you want

Interview Question

Why might np.arange() produce an unexpected number of elements when used with a float step, compared to what you'd calculate by hand?

Hint: Think about floating-point precision, not a bug in NumPy.

np.arange() determines how many steps fit into the interval by essentially dividing the total range by the step size, and floating-point numbers can't always represent that division exactly, so the computed count can be off by one from the mathematically exact answer due to tiny rounding errors accumulating across the float arithmetic involved. This is a known, documented limitation, which is exactly why NumPy's own documentation recommends np.linspace() when you need a precise, guaranteed number of points instead of a specific step size.

Exercises

MediumPractice using np.arange() in a real scenario.
View Solution
import numpy as np

arr = np.arange(0, 10, 2)
print(arr)

Frequently Asked Questions

Why might np.arange() produce an unexpected number of elements when used with a float step, compared to what you'd calculate by hand?

np.arange() determines how many steps fit into the interval by essentially dividing the total range by the step size, and floating-point numbers can't always represent that division exactly, so the computed count can be off by one from the mathematically exact answer due to tiny rounding errors accumulating across the float arithmetic involved. This is a known, documented limitation, which is exactly why NumPy's own documentation recommends np.linspace() when you need a precise, guaranteed number of points instead of a specific step size.

Related Functions

np-linspacenp-logspacerange()