what's the python for this C statement?

Hrvoje Niksic hniksic at xemacs.org
Mon Oct 20 06:34:11 EDT 2008


Michele <michele at nectarine.it> writes:

> Hi there,
> I'm relative new to Python and I discovered that there's one single way
> to cycle over an integer variable with for:
> for i in range(0,10,1)

Please use xrange for this purpose, especially with larger
iterations.  range actually allocates a sequence.

> However, how this C statement will be translated in Python?
>
> for (j = i = 0; i < (1 << H); i++)

If H doesn't change during the loop, you can use:

j = 0
for i in xrange(1 << H):
    ...

If H can change, simply rewrite it into an obvious 'while' loop.



More information about the Python-list mailing list