Iteration, while loop, and for loop

Elizabeth Weiss cake240 at gmail.com
Tue Jun 28 08:36:25 EDT 2016


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 + "!")
   counter=counter + 1



Both of these result in the same answer. 
I do not understand the second code. What is counter?
Why do we use this code if we can use the simpler for loop? 

If you could please explain the second code step by step that would be great!

Thank you!!!


More information about the Python-list mailing list