Re: Iteration, while loop, and for loop

2016-10-27 Thread Veek M
Elizabeth Weiss wrote: > words=["hello", "world", "spam", "eggs"] > counter=0 > max_index=len(words)-1 > > while counter<=max_index: > word=words[counter] > print(word + "!") > counter=counter + 1 while 0 < 10: get 0'th element do something with element increment 0 to 1 (repeat)

Re: Iteration, while loop, and for loop

2016-06-30 Thread jfong
Steven D'Aprano at 2016/6/30 7:59:40AM wrote: > py> mi = list('bananas') > py> for char in mi: > ... if char == 'a': > ... mi.extend(' yum') > ... print(char, end='') > ... else: # oh no, the feared for...else! > ... # needed to prevent the prompt overwriting the output >

Re: Iteration, while loop, and for loop

2016-06-30 Thread Ian Kelly
On Wed, Jun 29, 2016 at 5:59 PM, Steven D'Aprano wrote: > But there's no need to go to such effort for a mutable iterator. This is > much simpler: > > py> mi = list('bananas') > py> for char in mi: > ... if char == 'a': > ... mi.extend(' yum') > ...

Re: Iteration, while loop, and for loop

2016-06-30 Thread Ian Kelly
On Wed, Jun 29, 2016 at 5:59 PM, Steven D'Aprano wrote: > I'm curious what REPL you are using, because in the vanilla Python > interactive interpreter, the output if over-written by the prompt. That is, > what I see in Python 3.6 is: > > py> nas yum yum yumpy> > > unless I

Re: Iteration, while loop, and for loop

2016-06-30 Thread Tim Chase
On 2016-06-30 09:59, Steven D'Aprano wrote: > But there's no need to go to such effort for a mutable iterator. > This is much simpler: > > py> mi = list('bananas') > py> for char in mi: > ... if char == 'a': > ... mi.extend(' yum') > ... print(char, end='') > ... else:

Re: Iteration, while loop, and for loop

2016-06-29 Thread Steven D'Aprano
On Thu, 30 Jun 2016 01:29 am, Ian Kelly wrote: > On Tue, Jun 28, 2016 at 11:58 AM, Grant Edwards > wrote: [...] >>> But then, if you wrap up your "while" loop as a generator that yields >>> things, you can then use it in a "for" loop which seems to me like >>> the

Re: Iteration, while loop, and for loop

2016-06-29 Thread Ian Kelly
On Tue, Jun 28, 2016 at 11:58 AM, Grant Edwards wrote: > On 2016-06-28, Tim Chase wrote: >> On 2016-06-29 01:20, Steven D'Aprano wrote: >>> While loops are great for loops where you don't know how many >>> iterations there will be but you

Re: Iteration, while loop, and for loop

2016-06-29 Thread Lawrence D’Oliveiro
On Wednesday, June 29, 2016 at 1:30:04 AM UTC+12, BartC wrote: > I don't know if that helps; I've never heard of an induction variable. Perhaps it’s just a computability-theoretic way of saying “a variable whose value each time round the loop is a function of its value on the previous

Re: Iteration, while loop, and for loop

2016-06-28 Thread Grant Edwards
On 2016-06-28, Tim Chase wrote: > On 2016-06-29 01:20, Steven D'Aprano wrote: >> While loops are great for loops where you don't know how many >> iterations there will be but you do know that you want to keep >> going while some condition applies: >> >> while there

Re: Iteration, while loop, and for loop

2016-06-28 Thread Tim Chase
On 2016-06-29 01:20, Steven D'Aprano wrote: > While loops are great for loops where you don't know how many > iterations there will be but you do know that you want to keep > going while some condition applies: > > while there is still work to be done: > do some more work I find this

Re: Iteration, while loop, and for loop

2016-06-28 Thread Jussi Piitulainen
Steven D'Aprano writes: > While loops are great for loops :) Thanks, I needed the laugh. > where you don't know how many iterations there will be but you do know > that you want to keep going while some condition applies: (Just keeping a bit of context so it doesn't seem like I'm laughing at

Re: Iteration, while loop, and for loop

2016-06-28 Thread Steven D'Aprano
On Tue, 28 Jun 2016 10:36 pm, Elizabeth Weiss wrote: > Why do we use this code if we can use the simpler for loop? Nobody with any sense would use the more complex while loop when the for loop does the same thing. While loops are great for loops where you don't know how many iterations there

Re: Iteration, while loop, and for loop

2016-06-28 Thread Michael Selik
On Tue, Jun 28, 2016 at 9:26 AM Joseph Lee wrote: > > -Original Message- > From: Michael Selik > Sent: Tuesday, June 28, 2016 6:16 AM > > MS: You should not. Use the first version, it's much better. Python > for-loops are preferable to while-loops. > > JL: For

Re: Iteration, while loop, and for loop

2016-06-28 Thread Michael Selik
On Tue, Jun 28, 2016 at 9:34 AM BartC wrote: > On 28/06/2016 14:15, Michael Selik wrote: > > On Tue, Jun 28, 2016 at 8:41 AM Elizabeth Weiss > wrote: > > > >> I do not understand the second code. What is counter? > >> > > > > It looks like someone wanted to

Re: Iteration, while loop, and for loop

2016-06-28 Thread Jussi Piitulainen
Elizabeth Weiss writes: [- -] > What I do not understand is: > > words=["hello", "world", "spam", "eggs"] > counter=0 > max_index=len(words)-1 > > while counter<=max_index: >word=words[counter] >print(word + "!") >counter=counter + 1 # make it so that counter == 0 counter=0 # make

Re: Iteration, while loop, and for loop

2016-06-28 Thread BartC
On 28/06/2016 14:15, Michael Selik wrote: On Tue, Jun 28, 2016 at 8:41 AM Elizabeth Weiss 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 I

RE: Iteration, while loop, and for loop

2016-06-28 Thread Joseph Lee
Hi, Answers inline. -Original Message- From: Python-list [mailto:python-list-bounces+joseph.lee22590=gmail@python.org] On Behalf Of Michael Selik Sent: Tuesday, June 28, 2016 6:16 AM To: Elizabeth Weiss <cake...@gmail.com>; python-list@python.org Subject: Re: Iteration, whil

Re: Iteration, while loop, and for loop

2016-06-28 Thread BartC
On 28/06/2016 13:36, Elizabeth Weiss wrote: I understand this code: words=["hello", "world", "spam", "eggs"] for words in words print(word + "!") What I do not understand is: words=["hello", "world", "spam", "eggs"] counter=0 max_index=len(words)-1 while counter<=max_index:

Re: Iteration, while loop, and for loop

2016-06-28 Thread Michael Selik
On Tue, Jun 28, 2016 at 8:41 AM Elizabeth Weiss 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 > Why do we use this code if we can use the

Iteration, while loop, and for loop

2016-06-28 Thread Elizabeth Weiss
I understand this code: words=["hello", "world", "spam", "eggs"] for words in words print(word + "!") What I do not understand is: words=["hello", "world", "spam", "eggs"] counter=0 max_index=len(words)-1 while counter<=max_index: word=words[counter] print(word + "!")