My Coding >
Programming language >
Python >
Python FAQ >
Python: How to make iterations with fractional step
Python: How to make iterations with fractional stepTo make iterations with integer step in Python you can use function range, which can take only integer parameters. To make iterations over fractional parameters, it is necessary to use numpy.arange() function or use some trick with range in this example I will print all numbers from 0 ti 5.0 with step 0.2 Numpy fractional stepThis is a bit slower version, because you need to load Numpy library first, and then generate numpy structure with function arange().
Range with fractional steprange() can only works with integer parameters, so it is necessary to make a conversion, but I do recommend to use this version of iterations.
In range() you need to show how many steps to do: int(top/step), and then, recalculate step to real value i*step In both cases, output will be identical:
|
Last 10 artitles
9 popular artitles
|
|
© 2020 MyCoding.uk -My blog about coding and further learning. This blog was writen with pure Perl and front-end output was performed with TemplateToolkit. |