for loops | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

for loops

words = ["hello", "world", "spam", "eggs"] counter = 0 max_index = len(words) - 1 while counter <= max_index: word = words[counter] print(word + "!") counter = counter + 1 what does the [counter] in the 6th line represent? or why do we have to put it there? .please explain.

8th Oct 2018, 9:17 PM
Aws Poseidon
Aws Poseidon - avatar
2 Answers
0
Just to add to what 「HAPPY TO HELP」 said: 'words' is a list. In lists you use: list_name[index] to access the values in the list. In the code you mention, counter is used as the index for accessing the elements of list 'words'. When counter = 0 you get the first element, when it is 1, the second ...
8th Oct 2018, 9:49 PM
Ulisses Cruz
Ulisses Cruz - avatar
0
thanks guys ... now i see it 😅
8th Oct 2018, 10:35 PM
Aws Poseidon
Aws Poseidon - avatar