Pyderman
Pyderman

Reputation: 16189

Why does np.arange(10,1, 1) produce an empty array?

Why does np.arange(10,1, 1) produce an empty array, whereas np.arange(10,1, -1), on the other hand, produces what one would expect (an array from 10 down)?

Upvotes: 0

Views: 1450

Answers (1)

hpaulj
hpaulj

Reputation: 231335

What do you expect np.arange(10,1,1) to produce? You start at 10, step by 1, and stop when the result is 1 (or more). Same reasoning is used in Python range

Values are generated within the half-open interval [start, stop) (in other words, the interval including start but excluding stop). For integer arguments the function is equivalent to the Python built-in range <http://docs.python.org/lib/built-in-funcs.html>_ function, but returns an ndarray rather than a list.

for range:

If step is positive, the last element is the largest start + i * step less than stop;

>>> range(1, 0)
[]

Compare x[::2] with x[::-2] for some array or list to see the usefulness of paying attention to the sign of the step.

Upvotes: 2

Related Questions