Iteration, while loop, and for loop

Joseph Lee joseph.lee22590 at gmail.com
Tue Jun 28 09:26:30 EDT 2016


Hi,
Answers inline.

-----Original Message-----
From: Python-list
[mailto:python-list-bounces+joseph.lee22590=gmail.com at python.org] On Behalf
Of Michael Selik
Sent: Tuesday, June 28, 2016 6:16 AM
To: Elizabeth Weiss <cake240 at gmail.com>; python-list at python.org
Subject: Re: Iteration, while loop, and for loop

On Tue, Jun 28, 2016 at 8:41 AM Elizabeth Weiss <cake240 at gmail.com> wrote:

> I do not understand the second code. What is counter?
>

It looks like someone wanted to make a loop induction variable.
https://en.wikipedia.org/wiki/Induction_variable

JL: Or access the array item via indexing.

> Why do we use this code if we can use the simpler for loop?
>

You should not. Use the first version, it's much better. Python for-loops
are preferable to while-loops.

JL: Indexing is useful if one wishes to assign something different to the
position represented by container[index]. But if you want a more elegant way
to access the actual object in question, then using the first fragment is
better (and more readable). For the most part, for loops are better, but
there are times when while loops are preferable such as when you don't know
the size of the input beforehand.

Cheers,
Joseph

-
https://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list