0
Can anybody explain me how this code works explaining how the lists are used
5 Réponses
+ 1
#explanation is in comments, read ones
#words is defined as a list of 4 elements by
words = ["hello", "world", "spam", "eggs"]
#for start index of list, ie words[0] ="hello"
counter = 0
#for last max index of list,
max_index = len(words) - 1
#while 0 to max_index reached, loop runs, and loop exits when counter is greater than maz_index
while counter <= max_index:
#extracting list item at index ' counter' into word
word = words[counter]
print(word + "!") #displaying
#incrementing counter to take next index for next value of list item.. So counter values are varies from 0,1,2,3,4
counter = counter + 1
Output:
hello!
world!
spam!
eggs!
Ifham Khwaja hope it clears. If not mention where you not understood..
It just displaying words[0] to words[3].
+ 1
Ifham Khwaja
Check this thread out
https://www.sololearn.com/Discuss/2400674/?ref=app
0
I did not understand this. What does extracting list item at index counter mean? What does counter variable mean
#extracting list item at index ' counter' into word
word = words[counter]
print(word + "!") #displaying
0
And what does word = words [counter] mean. Is it some kind of loop or list ? I am confused
0
Ifham Khwaja extracting list item at index counter mean "getting value from list at specified index by counter " as an its value..
What does counter variable mean '"you are initialized a variable "counter = 0" that variable i reffereing. If you take it as just cn then it is cn..
#extracting
word = words[counter];
here words is list and value at index "counter" is assigned to variable word.
Ex:
word = words[0] => then word ="hello".
word = words[1] => then word = "world"..
Hope it clears...