what's the python for this C statement?

Chris Rebert clp at rebertia.com
Mon Oct 20 06:08:22 EDT 2008


On Mon, Oct 20, 2008 at 2:56 AM, Michele <michele at nectarine.it> wrote:
> 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)

Actually, you want:

for i in range(10):

Since starting at 0 and using a step of 1 are the defaults.

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

There's no nice way to translate more complex 'for' statements like
that (unless you can calculate the end value somehow).
You basically just have to use a 'while' loop instead:

i = j = 0
while i < 1 << H:
    #body goes here
    i += 1

Since bit-shifting doesn't get used much in Python and iteration
through a generator or the items of a collection is more common, this
doesn't end up being a problem in practice.

Cheers,
Chris
-- 
Follow the path of the Iguana...
http://rebertia.com


>
> Thanks
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list