We usually use python range() function to create an integer sequence, however, this function can not use float number. If you want to use float number to create a float sequence, how to do? In this tutorial, we will introduce you how to do.
Syntax of range
range(start, stop[, step])
Here start, stop and step are all integer, you can not use a float number. For example:
X = range(0.1, 10.0, 0.2) print(X)
Run this python script, you will get error:
TypeError: ‘float’ object cannot be interpreted as an integer
How to create float range?
We can use numpy.arange() function.
Syntax of numpy.arange()
numpy.arange([start, ]stop, [step, ]dtype=None)
Here start, stop and step can be float number, here is an example:
import numpy as np X = np.arange(0.1, 10.0, 0.2) print(X)
Then you will get result like:
[0.1 0.3 0.5 0.7 0.9 1.1 1.3 1.5 1.7 1.9 2.1 2.3 2.5 2.7 2.9 3.1 3.3 3.5 3.7 3.9 4.1 4.3 4.5 4.7 4.9 5.1 5.3 5.5 5.7 5.9 6.1 6.3 6.5 6.7 6.9 7.1 7.3 7.5 7.7 7.9 8.1 8.3 8.5 8.7 8.9 9.1 9.3 9.5 9.7 9.9]