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 list comprehension with range
.
For instance, we write:
l = [x * 0.1 for x in range(0, 10)]
print(l)
to create a list of numbers from 0, 0.1, 0.2, … all the way to 0.9.
We create an iterator with range
from 0 and 10 to create an iterator that returns 0 to 9.
And then we multiply those entries by 0.1
Therefore, l
is [0.0, 0.1, 0.2, 0.30000000000000004, 0.4, 0.5, 0.6000000000000001, 0.7000000000000001, 0.8, 0.9]
.
Conclusion
To use a decimal range() step value with Python, we can use list comprehension with range
.