For and in | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

For and in

So the code goes like this: words = ["hello", "world", "spam", "eggs"] for word in words: print(word + "!") I was under the impression that “in” referenced something “in” the list, but “word” isn’t in the list? Is “for” assigning the “words” list to “word” for the print command? Please explain how you would to a toddler, I’m very new to this Thanks!

31st Dec 2019, 3:27 AM
Rocketeer
Rocketeer - avatar
2 Answers
+ 5
word is the variable you assign for objects in the list.
31st Dec 2019, 3:38 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 6
You could substitute 'x' for 'word' and 'y' for 'words' and get exactly the same output. It's just better coding style to use variable names that explain what's happening, which makes the code more readable.
31st Dec 2019, 4:53 AM
David Ashton
David Ashton - avatar