[Tutor] Losing the expressiveness of C's for-statement?/ RESEND with example

Stephen McInerney spmcinerney at hotmail.com
Tue Aug 7 11:24:25 CEST 2007

(Question as below)

Sorry I meant to pick a tangible example to focus the discussion:

This one cannot be (easily) translated to use Python's range() operator
for (i=30000; i>0; i=i/2) { ... }

So do you need to know a whole armory of other functions to use
to generate iterators, or just translate to a while-loop already?


>From: "Stephen McInerney" <spmcinerney at hotmail.com>
>To: tutor at python.org
>Subject: Losing the expressiveness of C's for-statement?
>Date: Tue, 07 Aug 2007 02:18:16 -0700
>Hi all,
>As the Python doc says: "The for statement in Python differs a bit from 
>what you may be used to in C or Pascal. Rather than giving the user the 
>ability to define both the iteration step and halting condition (as C), 
>Python's for statement iterates over the items of any sequence (a list or a 
>This is much poorer than C/C++, and does not allow for the step action to 
>be multiple actions, fn calls etc. - not straightforwardly anyway. (don't 
>take that as a challenge)
>- I know how to migrate to a while-loop, but I lament losing the very 
>compact expressiveness of:
>for (node=start; value<threshold && node!=end; node=node->next) { ... }
>- I figure the straightforward answer will be "use a while-loop, put the 
>iteration step at the end".
>- the fancy showoff answer will probably involve a funky iterator with 
>side-effects, or returning tuples.
>- what if the loop iteration step involves variables from within the 
>loop-body (e.g. as in quicksort stepsize);
>- what I'm trying to drive at here is the general solution of least 
>idiomaticity, not of greatest language-specific cleverness
>Any comments or article links? Also, it would be useful to improve the 
>Python tutorial on this.
>Since this is one area where Python is (syntactically) inferior to 

Find a local pizza place, movie theater, and more….then map the best route! 

More information about the Tutor mailing list