words = ["hello", "world", "spam", "eggs"] for word in words: print(word + "!") | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

words = ["hello", "world", "spam", "eggs"] for word in words: print(word + "!")

In this what does 'for word in words' mean?

27th Feb 2019, 1:36 AM
Monkey D. Luffy
Monkey D. Luffy - avatar
1 Answer
0
In this case, the variable "word" is meant to loop through each element in the "words" array until there are no more elements left to loop through. For the first 2 iterations, it would look like this: word = words[0] #Which is hello print(word + "!") #Outputs hello! word = words[1] #Word now equals world print(word + "!") #Outputs world!
27th Feb 2019, 2:07 AM
Faisal
Faisal - avatar