+2 votes
in Programming Languages by (56.7k points)
I want to find a list of evenly spaced numbers over a given interval. What Python function should I use?

1 Answer

+2 votes
by (73.8k points)
selected by
 
Best answer

You can use the linspace() function of Numpy. It returns the evenly spaced numbers over a given interval. You can provide the start, end, and count of numbers you want as arguments.

Here is an example:

>>> import numpy as np
>>> a=np.linspace(0, 1, 20)
>>> a
array([0.        , 0.05263158, 0.10526316, 0.15789474, 0.21052632,
       0.26315789, 0.31578947, 0.36842105, 0.42105263, 0.47368421,
       0.52631579, 0.57894737, 0.63157895, 0.68421053, 0.73684211,
       0.78947368, 0.84210526, 0.89473684, 0.94736842, 1.        ])
>>>

You can also use arrange() function of Numpy. You need to provide the start, end, and step size as arguments.

>>> np.arange(1, 20, 5)
array([ 1,  6, 11, 16])
>>>


...