Sometimes, we want to use a decimal range() step value with Python.
In this article, we’ll look at how to use a decimal range() step value with Python.
How to use a decimal range() step value with Python?
To use a decimal range() step value with Python, we can use NumPy’s linspace
method.
For instance, we write
np.linspace(0, 1, 11)
to create the [ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ]
NumPy array.
We can call linspace
with endpoint
set to False
to remove the endpoint value.
And we get
[ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
as a result.
Conclusion
To use a decimal range() step value with Python, we can use NumPy’s linspace
method.