How to Use the arange() Function in Python

Say you’re saving $250 a month through 2026 and want each month numbered so you can chart the balance. The arange function in Python does it in one call: np.arange(1, 13) gives you the months 1 to 12 as a NumPy array.

Unlike range(), it returns an array you can do math on, and it accepts decimal steps.

In this tutorial, I’ll show you start, stop, step, the float trap and reshape on the same savings data, tested on Python 3.12.5 and NumPy 2.5.3.

The sample savings data we’ll use with np.arange()

Every example uses one plan for a saver in Ohio:

  • Months 1 to 12, with a $250 deposit each month
  • Quarterly check-ins in months 3, 6, 9 and 12
  • Rates of 1.0%, 1.1% and 1.2% APY (the tricky one)

The rates use a decimal step, which matters later. For 1 to N without NumPy, see creating an array from 1 to N in Python.

np.arange() syntax and parameters

The full signature is np.arange(start, stop, step, dtype=None). Only stop is required:

ParameterDefaultWhat it does
start0First value, included
stoprequiredEnd value, not included
step1Gap between values; can be negative or a decimal
dtypeinferredType of the output array, like float

It returns a one-dimensional ndarray. For arrays in general, read how to create arrays in Python.

Method 1: Create whole-number sequences with np.arange(start, stop) (Recommended)

This is the form you’ll use most. Same savings data here: month numbers, then the running deposits.

Here’s the code:

import numpy as np

print(np.arange(12))          # stop only: 0 to 11
months = np.arange(1, 13)     # start and stop: 1 to 12
print(months)
print(np.arange(1, 12))       # stop is left out: no December

deposits = months * 250       # $250 per month, Jan to Dec 2026
print(deposits)
print(f"Saved by December: ${deposits[-1]:,.2f}")

Output:

[ 0  1  2  3  4  5  6  7  8  9 10 11]
[ 1  2  3  4  5  6  7  8  9 10 11 12]
[ 1  2  3  4  5  6  7  8  9 10 11]
[ 250  500  750 1000 1250 1500 1750 2000 2250 2500 2750 3000]
Saved by December: $3,000.00

You get months 1 to 12 and $3,000.00 saved by December.

Command Prompt output of numpy arange creating month numbers 1 to 12 and multiplying them by 250 dollars
np.arange(1, 13) gives months 1 to 12; a stop of 12 drops December.

How does this code work?

np.arange(12) treats 12 as the stop, so it runs 0 to 11.

np.arange(1, 13) fixes it: the stop is excluded, so write one past the last month. np.arange(1, 12) is the classic off-by-one bug.

months * 250 multiplies every element with no loop. The loop version is in for i in range in Python.

Method 2: Add a step to np.arange(), including negative steps

The third argument sets the gap. Same 12 months, now with check-ins and a countdown:

import numpy as np

checkins = np.arange(3, 13, 3)      # every third month
print("Check-ins:", checkins)

left = np.arange(12, 0, -3)         # count down, step is negative
print("Months left:", left)

print("Wrong sign:", np.arange(12, 0, 3))   # empty, no error

Output:

Check-ins: [ 3  6  9 12]
Months left: [12  9  6  3]
Wrong sign: []

That gives check-ins at 3, 6, 9 and 12, and a countdown of 12, 9, 6 and 3.

Command Prompt output of numpy arange with a step of 3 and a negative step of -3, plus an empty array when the sign is wrong
Steps of 3 and -3 work; the wrong sign returns an empty array.

How does this code work?

np.arange(3, 13, 3) adds 3 until the next value would hit 13, which keeps month 12 in.

For a countdown, start high and use a negative step. It never reaches 0, since the stop is excluded.

So here’s the catch: np.arange(12, 0, 3) can’t reach 0 by adding 3, so it returns an empty array, not an error. More in reversing a range in Python.

Method 3: Why does np.arange() return an extra value with float steps?

Now the rates. I want 1.0%, 1.1% and 1.2%, so stopping at 1.3 with a 0.1 step looks right:

import numpy as np

rates = np.arange(1.0, 1.3, 0.1)    # want 1.0, 1.1 and 1.2 % APY
print(rates, len(rates))
print(repr(rates[-1]))              # the "stop" sneaked in

print(np.arange(10, 13) / 10)       # fix 1: whole steps, then scale
print(np.linspace(1.0, 1.2, 3))     # fix 2: ask for 3 values

Output:

[1.  1.1 1.2 1.3] 4
np.float64(1.3000000000000003)
[1.  1.1 1.2]
[1.  1.1 1.2]

You get four rates, and the last is 1.3000000000000003.

Command Prompt output of numpy arange with a 0.1 float step returning four values including the stop, then two fixes
The 0.1 step adds an extra rate; both fixes return exactly three.

How does this code work?

NumPy sets the length to ceil((stop - start) / step). 0.1 isn’t exact in binary, so that’s a hair above 3 and rounds up to 4.

Fix 1 divides whole numbers by 10. Fix 2, np.linspace(), asks for a count instead of a step. Compare them in NumPy linspace vs arange.

Important: the NumPy docs recommend linspace for non-integer steps. The why is in floating-point numbers in Python.

Method 4: Set the dtype and reshape np.arange() output

Sometimes you need floats or a grid. Same deposit numbers, by quarter:

import numpy as np

months = np.arange(1, 13, dtype=float)
print(months)

grid = (np.arange(1, 13) * 250).reshape(4, 3)    # one row per quarter
print(grid)
print("End of each quarter:", grid[:, -1])

try:
    range(1, 2, 0.5)
except TypeError as err:
    print("range():", err)

Output:

[ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12.]
[[ 250  500  750]
 [1000 1250 1500]
 [1750 2000 2250]
 [2500 2750 3000]]
End of each quarter: [ 750 1500 2250 3000]
range(): 'float' object cannot be interpreted as an integer

The balance at the end of each quarter comes out to $750, $1,500, $2,250 and $3,000.

Command Prompt output of numpy arange with dtype float, reshaped into a 4 by 3 quarterly grid of balances
reshape(4, 3) puts each quarter on its own row; range() rejects 0.5.

How does this code work?

dtype=float stores 1.0, 2.0 and so on instead of integers.

reshape(4, 3) makes four rows of three months, and grid[:, -1] takes each row’s last month. The sizes must multiply to 12; see reshaping a NumPy array.

Pro Tip: the last line shows range() can’t do decimals; it raises a TypeError on 0.5.

np.arange() vs range(): which should you use?

NeedUseWhy
Loop a set number of timesrange()No NumPy needed, uses almost no memory
Math on every valuenp.arange()Returns an array, so * 250 just works
Decimal stepnp.linspace()Exact count, no surprise extra value

Things to Keep in Mind with the arange Function

  • The stop is never included: use np.arange(1, 13) for 1 to 12.
  • A step pointing away from the stop returns an empty array. Check the sign.
  • Float steps can add a value. Use np.linspace() instead.
  • reshape() sizes must multiply to the array length.

Working with NumPy arrays next? These pick up where arange leaves off:

Frequently asked questions

What does arange() do in Python?

It returns a NumPy array of evenly spaced values from start up to, but not including, stop.

Is the stop value included in np.arange()?

No. np.arange(1, 13) ends at 12.

Can np.arange() use a decimal step?

Yes, but rounding can add a value; np.linspace() is safer.

What is the difference between range() and np.arange()?

range() is built in and integer-only; np.arange() returns an array and accepts floats.

How do I make a 2D array with arange?

Call .reshape() on it, like np.arange(12).reshape(4, 3).

Where is numpy.arange documented?

See numpy.arange in the NumPy reference.