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!
2 Answers
+ 5
word is the variable you assign for objects in the list.
+ 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.