Next / Previous / Contents / Shipman's homepage

5.2. The arange() function: Arithmetic progression

Use the np.arange() function to build a vector containing an arithmetic progression such as [0.0 0.1 0.2 0.3].

>>> print np.arange(0.0, 0.4, 0.1)
[ 0.   0.1  0.2  0.3]
>>> print np.arange(5.0, -0.5, -0.5)
[ 5.   4.5  4.   3.5  3.   2.5  2.   1.5  1.   0.5  0. ]

Here is the general form:

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

The first value in the sequence.

stop

The limiting value: the last element of the sequence will never be greater than or equal to this value (assuming that the step value is positive; for negative step values, the last element of the sequence will always be greater than the stop value).

>>> print np.arange(1.0, 4.0)
[ 1.  2.  3.]

If you omit the stop value, you will get a sequence starting at zero and using start as the limiting value.

>>> print np.arange(4)
[0 1 2 3]
step

The common difference between successive values of the array. The default value is one.

dtype

Use this argument to force representation using a specific type.

>>> print np.arange(10)
[0 1 2 3 4 5 6 7 8 9]
>>> print np.arange(10, dtype=np.float_)
[ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9.]