I didnt understand why "word" is there in the 2nd line ? Its not even defined.. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I didnt understand why "word" is there in the 2nd line ? Its not even defined..

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

4th May 2020, 6:17 PM
Chinmaya Behera
Chinmaya Behera - avatar
5 Answers
+ 3
word is the name of a for loop varibale. It does not need to be defined. it holds in easch iteration of the loop one word, starting from the left side of the list words. next iteration will give next element from words list, and so on. The loop has as many iterations as the list has elements. inside the loop you can no do something with the content of word.
4th May 2020, 6:23 PM
Lothar
Lothar - avatar
0
Becoz it is words not not word I thunk u forget s in last of word
4th May 2020, 6:21 PM
Abhay
Abhay - avatar
0
word could be anything. for i in words: print(i + '!') Would do the same, its just assigning a variable to each word on the fly. wich is overwritten with each word every turn in the iteration
4th May 2020, 6:21 PM
Slick
Slick - avatar
0
Word represents where you are in the loop so that you can make the loop do different things for each run through.
4th May 2020, 6:22 PM
evan
evan - avatar
0
It's basically used to iterate in loop it can be any name variable , depends on u.
4th May 2020, 6:22 PM
Raj Laxmi Verma
Raj Laxmi Verma - avatar