Categories
Python Answers

How to use a decimal range() step value with Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *