26

I'm trying to loop from 0 to 1 using step sizes of 0.01 (for example). How would I go about doing this? The for i in range(start, stop, step) only takes integer arguments so floats won't work.

efficiencyIsBliss
  • 3,043
  • 7
  • 38
  • 44

7 Answers7

31
for i in [float(j) / 100 for j in range(0, 100, 1)]:
    print i
Santa
  • 11,381
  • 8
  • 51
  • 64
  • Bear in mind that the last printed value here is 0.99, and not 1.0, but this is only because the last value returned by `range` is 99 and not 100. – Asclepius Dec 14 '14 at 18:56
  • 2
    If you want 0.00 to 1.00 inclusive, then use: `range(0, 101, 1)`. – Santa Jun 19 '15 at 20:26
  • Note that this has no reason not to use a generator comprehension, i.e. using parentheses as in `for i in (float(j) / 100 for j in range(100)):` -- this creates a lazy generator rather than building a whole list before even starting the loop. –  Jul 30 '18 at 00:32
6

Avoid compounding floating point errors with this approach. The number of steps is as expected, while the value is calculated for each step.

def drange2(start, stop, step):
    numelements = int((stop-start)/float(step))
    for i in range(numelements+1):
            yield start + i*step
Usage:

for i in drange2(0, 1, 0.01):
    print i
WombatPM
  • 2,561
  • 2
  • 22
  • 22
1

you can use list comprehensions either:

print([i for i in [float(j) / 100 for j in range(0, 100, 1)]])

if you want control over printing i then do something like so:

print(['something {} something'.format(i) for i in [float(j) / 100 for j in range(0, 100, 1)]])

or

list(i for i in [float(j) / 100 for j in range(0, 100, 1)])
1

One option:

def drange(start, stop, step):
    while start < stop:
            yield start
            start += step

Usage:

for i in drange(0, 1, 0.01):
    print i
carl
  • 49,756
  • 17
  • 74
  • 82
  • -1: This could have a problem if the `step` cannot be exactly represented by a floating point number. For example the last element for `drange(0,13,0.13)` is 12.870000000000024 (**100** elements), and the last element for `drange(0,11,0.11)` is 10.999999999999995 (**101** elements). – eumiro Feb 08 '11 at 07:28
  • Even simpler: `list(drange(0,10,1))` looks like it behaves like `range`: `0, 1, 2, 3, 4, 5, 6, 7, 8, 9]` but `list(drange(0,1,0.1))` gives an extra element: `[0, 0.1, 0.2, 0.30000000000000004, 0.4, 0.5, 0.6, 0.7, 0.7999999999999999, 0.8999999999999999, 0.9999999999999999]`. – Duncan Feb 08 '11 at 12:19
1

I would say the best way is using numpy array.

If you want to to loop from -2 thru +2 with increment of 0.25 then this is how I would do it:

Start = -2
End = 2
Increment = 0.25
Array =  np.arange(Start, End, Increment)

for i in range(0, Array.size):
    print (Array[i])

0

Well, you could make your loop go from 0 to 100 with a step the size of 1 which will give you the same amount of steps. Then you can divide i by 100 for whatever you were going to do with it.

  • @James Yes, we could also just make an infinite while loop, and maintain the counters inside the loop. However, I'm trying to learn Python and I figured this would be good to know. – efficiencyIsBliss Feb 08 '11 at 05:14
  • 2
    One issue here is that initializing a variable to 0.0 and adding 0.01 to it 100 times will not necessarily result in exactly 1.0. So the "right" way to do this depends a lot on how the values are to be used. – garyjohn Feb 08 '11 at 05:34
  • @efficiencyIsBliss - first thing you need to learn is that floats are not as exact as you may expect them to be. Sort of. Go to the Python interpreter, type 'a = 0.4' then enter, then type 'a' and enter. – dotalchemy Feb 08 '11 at 07:15
-1

your code

for i in range(0,100,0.01):

can be achieved in a very simple manner instead of using float

for i in range(0,10000,1):

if you are very much concerned with float then you can go with https://stackoverflow.com/a/4935466/2225357

Community
  • 1
  • 1