How do I use a decimal step value for range()?
You can use the numpy
library's arange()
function to specify a decimal step value for the range. numpy.arange(start, stop, step)
generates an array with a range of values, similar to the built-in range()
function.
For example, to generate a range of values from 0 to 1 with a step of 0.1, you would use the following code:
import numpy as np
for i in np.arange(0, 1, 0.1):
print(i)
This will output:
0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9
Watch a video course
Python - The Practical Guide
Additionally, python 3.x range()
function also support float step value and it works the same way as numpy.arange() function
This will output:
0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9