+ 1
How am i suppoesed understand the square brackets around counter in the line, “word = words[counter]”?
words = ["hello", "world", "spam", "eggs"] counter = 0 max_index = len(words) - 1 while counter <= max_index: word = words[counter] print(word + "!") counter = counter + 1
2 Respostas
+ 11
You put the index number inside the square brackets to indicate which element of a list you want to have returned.
In this case, word will be equal to counter-th element of the words list (counting Python-wise, starting from index 0).
If in the loop you increase counter by one each time, then word will be equal to each of the elements of the words list at each loop. This process is called "iteration". And what you do with the list - you iterate (through) it.
+ 2
It's indexing. It gets the object at that position in the list