I want to use the traditional C-style for loop in Python. I want to loop through characters of a string, but also know what it is, and be able to jump through characters (e.
The Python for
loop always has foreach semantics. You can, however, do this:
for i in xrange(10):
print i
This is very much like a C for loop. xrange
(or range
, as it was renamed in Python 3) is a constructor for a Python object that iterates through a range of numbers. See the docs for more information.
In C
:
for(int i=0; i<9; i+=2)
{
dosomething(i);
}
In python3
:
for i in range(0, 9, 2):
dosomething(i)
You just express the same idea in different languages.
For all the ones going "Why?", here's an example where a C-style loop would be helpful:
remaining_retries = 3
for i in range(num_runs):
success = do_run()
if not success and remaining_retries > 0:
i = i - 1
remaining_retries = remaning_retries - 1
for i in range(n):
...is the Python equivalent of the C...
for (i = 0; i < n; i++){
Or well, you can use:
for i in range(a, n, s):
...which is equivalent to...
for (i = a; i < n; i+=s){
I provide the following entirely facetious solution by way of protest. Note that 'break' and 'continue' will not work. Also note that the loop body must not be indented.
class For:
def __init__(self, **loop_vars):
self.loop_vars = loop_vars
def __call__(self, arg):
if not hasattr(self, 'condition'):
self.condition = arg
return self
if not hasattr(self, 'update'):
self.update = arg
return self
while eval(self.condition, self.loop_vars, self.loop_vars):
exec arg in self.loop_vars
exec self.update in self.loop_vars
For(i = 1, j = 1)('i * j < 50')('i += 1; j += 1')('''
print i, j
''')
The simple answer is that there is no simple, precise equivalent of C's for
statement in Python. Other answers covered using a Python for
statement with a range. If you want to be able to modify the loop variable in the loop (and have it affect subsequent iterations), you have to use a while
loop:
i = 0
while i < 7:
if someCondition(i):
i = 5
i += 1
But in that loop, a continue
statement will not have the same effect that a continue
statement would have in a C for
loop. If you want continue
to work the way it does in C, you have to throw in a try
/finally
statement:
i = 0
while i < 7:
try:
if someCondition(i):
i = 5
elif otherCondition(i):
continue
print 'i = %d' % i
finally:
i += 1
As you can see, this is pretty ugly. You should look for a more Pythonic way to write your loop.
This just occurred to me... there is a complicated answer that lets you use a normal Python for
loop like a C-style loop, and allows updating the loop variable, by writing a custom iterator. I wouldn't recommend this solution for any real programs, but it's a fun exercise.
Example “C-style” for loop:
for i in forrange(10):
print(i)
if i == 3:
i.update(7)
Output:
0
1
2
3
8
9
The trick is forrange
uses a subclass of int
that adds an update
method. Implementation of forrange
:
class forrange:
def __init__(self, startOrStop, stop=None, step=1):
if step == 0:
raise ValueError('forrange step argument must not be zero')
if not isinstance(startOrStop, int):
raise TypeError('forrange startOrStop argument must be an int')
if stop is not None and not isinstance(stop, int):
raise TypeError('forrange stop argument must be an int')
if stop is None:
self.start = 0
self.stop = startOrStop
self.step = step
else:
self.start = startOrStop
self.stop = stop
self.step = step
def __iter__(self):
return self.foriterator(self.start, self.stop, self.step)
class foriterator:
def __init__(self, start, stop, step):
self.currentValue = None
self.nextValue = start
self.stop = stop
self.step = step
def __iter__(self): return self
def next(self):
if self.step > 0 and self.nextValue >= self.stop:
raise StopIteration
if self.step < 0 and self.nextValue <= self.stop:
raise StopIteration
self.currentValue = forrange.forvalue(self.nextValue, self)
self.nextValue += self.step
return self.currentValue
class forvalue(int):
def __new__(cls, value, iterator):
value = super(forrange.forvalue, cls).__new__(cls, value)
value.iterator = iterator
return value
def update(self, value):
if not isinstance(self, int):
raise TypeError('forvalue.update value must be an int')
if self == self.iterator.currentValue:
self.iterator.nextValue = value + self.iterator.step