Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

for loop in python with decimal number as step [duplicate]

I want to make a for loop that looks like this:

for x in range(0, 1, 0.1):
    print(x)

obviously it throws this error:

Traceback (most recent call last): File "", line 1, in for i in range(0, 1, 0.1): TypeError: 'float' object cannot be interpreted as an integer

So it there a way in python 3.6 to make a for loop with floating point?

like image 933
Tissuebox Avatar asked Dec 06 '22 12:12

Tissuebox


1 Answers

use numpy's arange instead of range:

import numpy as np
for x in np.arange(0, 1, 0.1):
    print(x)
like image 119
sacuL Avatar answered Jan 08 '23 13:01

sacuL