Can anybody explain me how this code works explaining how the lists are used | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can anybody explain me how this code works explaining how the lists are used

https://code.sololearn.com/ci4UMF2Pxdw8/?ref=app

10th Aug 2020, 7:08 PM
Ifham Khwaja
5 Answers
+ 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].
10th Aug 2020, 7:15 PM
Jayakrishna 🇮🇳
10th Aug 2020, 7:22 PM
Tomiwa Joseph
Tomiwa Joseph - avatar
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
11th Aug 2020, 4:07 AM
Ifham Khwaja
0
And what does word = words [counter] mean. Is it some kind of loop or list ? I am confused
11th Aug 2020, 4:21 AM
Ifham Khwaja
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...
12th Aug 2020, 2:29 PM
Jayakrishna 🇮🇳