Changing step in Python loop

Viewed 75391

In Python 2.7 I want to modify the step of a for loop in function of the specifics conditions satisfied in the loop. Something like this:

step = 1
for i in range(1, 100, step):
    if ...... :
        step = 1
        #do stuff
    else:
        step = 2
        #do other stuff

but it seems that this can't be done, step is always 1.

Thanks.

6 Answers

np.arrange creates a (numpy) array with increasing values. The for-loop goes through all the values of the array. (Numpy is a powerful library for computations with numerical arrays)

import numpy as np
for i in np.arange(start,stop,stepwidth):
    # your stuff
Related