[Tutor] 'for' loops

W W srilyk at gmail.com
Tue Dec 2 01:53:28 CET 2008


On Mon, Dec 1, 2008 at 6:44 PM, WM. <wferguson1 at socal.rr.com> wrote:

> I recently asked a question about 'for' loops, expecting them to be similar
> to 'for-next' loops. I have looked at several on-line tutors but  am still
> in the dark about what 'for' loops do.
> Does anyone have a plain English about the use of 'for' loops?
> Are 'while' loops the only way Python runs a sub-routine over & over?


For loops are mainly used when you want a specific number of iterations,
such as looping over the elements of a list. In C/C++ you would do something
like this:

int myarray[] = {1, 2, 3, 4, 5};
for(int x = 0; x < 5; x++)
    printf("%d", myarray[x])

In python it would be much cleaner:

myarray = [1, 2, 3, 4, 5]
for x in myarray:
    print x

HTH,
Wayne

-- 
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn't. - Primo Levi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20081201/5ee06363/attachment.htm>


More information about the Tutor mailing list