For loop with custom steps in python
for i in range(0, 10, 2):
print(i)
>>> 0
>>> 2
>>> 4
>>> 6
>>> 8
http://docs.python.org/2/library/functions.html
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1, 11)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> range(0, 30, 5)
[0, 5, 10, 15, 20, 25]
>>> range(0, 10, 3)
[0, 3, 6, 9]
First and foremost: Python for
loops are not really the same thing as a C for
loop. They are For Each loops instead. You iterate over the elements of an iterable. range()
generates an iterable sequence of integers, letting you emulate the most common C for
loop use case.
However, most of the time you do not want to use range()
. You would loop over the list itself:
for elem in reversed(some_list):
# elem is a list value
If you have to have a index, you usually use enumerate()
to add it to the loop:
for i, elem in reversed(enumerate(some_list)):
# elem is a list value, i is it's index in the list
For really 'funky' loops, use while
or create your own generator function:
def halved_loop(n):
while n > 1:
yield n
n //= 2
for i in halved_loop(10):
print i
to print 10
, 5
, 2
. You can extend that to sequences too:
def halved_loop(sequence):
n = -1
while True:
try:
yield sequence[n]
except IndexError:
return
n *= 2
for elem in halved_loop(['foo', 'bar', 'baz', 'quu', 'spam', 'ham', 'monty', 'python']):
print elem
which prints:
python
monty
spam
foo
For your exact example, you probably wouldn't use a for loop at all, but a while loop:
w = n
while w > 1:
do stuff
w = w / 2
You need to use a generator. You could implement this as follows:
def stepDown(n):
while n>1:
yield n
n = n/2
for i in stepDown(n):
print i # or do whatever else you wish.
Note that this generalizes easily to other complicated patterns you may have in mind.
Something like for i in [n/(2**j) for j in range(int(math.log(n))+1)]