You can use a range with a step size of 2:
Python 2
for i in xrange(0,10,2):
print(i)
Python 3
for i in range(0,10,2):
print(i)
Note: Use xrange in Python 2 instead of range because it is more efficient as it generates an iterable object, and not the whole list.
You can use a range with a step size of 2:
Python 2
for i in xrange(0,10,2):
print(i)
Python 3
for i in range(0,10,2):
print(i)
Note: Use xrange in Python 2 instead of range because it is more efficient as it generates an iterable object, and not the whole list.
You can also use this syntax (L[start:stop:step]):
mylist = [1,2,3,4,5,6,7,8,9,10]
for i in mylist[::2]:
print i,
# prints 1 3 5 7 9
for i in mylist[1::2]:
print i,
# prints 2 4 6 8 10
Where the first digit is the starting index (defaults to beginning of list or 0), 2nd is ending slice index (defaults to end of list), and the third digit is the offset or step.
you would need to increment step manually which can be done using a while loop. checkout difference between while and for loop.
The for statement iterates through a collection or iterable object or generator function.
The while statement simply loops until a condition is False.
if you use a while loop your code would look something like this:
step = 1
i = 1
while i < 100:
if ...... :
step = 1
#do stuff
else:
step = 2
#do other stuff
i = i + step
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
The third parameter of range is indeed step. But you should use it with a computed value like:
for i in range(1,1000,2):
#statements
The reason why your i += i didn't worked is because, under the hood, the for-loop is executing something similar to i = next(...) at the end of an iteration, overiding your last increment.
Edit
You can achieve the desired effect using a generator, but it kinda kills the "trying to avoid while-loops" thing:
def myrange(start, stop):
i = start
while i < stop:
yield i
i += i
for i in myrange(1, 1000):
print(i)
Anyway, while-loops are perfectly valid constructs and I’d personnally go with one in this case. Do not forget that the for-loop has a rather different semantic in python than in both languages you’re used to. So trying to use a for-loop because you are able to do so with javascript seems like a bad idea if all what you need is a while-loop.
range can step by a fixed amount, but not a variable amount. Use a while-loop to increment i by i:
i += i
You could replace the while-loop with an iterator, such as:
import itertools as IT
for i in (2**i for i in IT.count()):
if i >= 1000000000: break
print("when x is", i, ":")
binary(i)
but I don't think this has any advantage over a simple while-loop.
» pip install steptools
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