Python: for loops | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Python: for loops

Below is the code, I don't understand how and why "word" came out of nowhere. Does "for' accomplishes the same thing as "while"? words = ["hello", "world", "spam", "eggs"] for word in words: print(word + "!")

6th Aug 2020, 4:06 AM
Adam Haller
Adam Haller - avatar
3 Answers
+ 4
"for word in words" Is a specific python syntax to access all items of a list one by one. You can use any variable name instead of "word". You can also replace it by "for i in range (len(words))" It will do the same. But "for word in words" looks clean and easier to understand
6th Aug 2020, 4:15 AM
Artem 🇺🇦
Artem 🇺🇦 - avatar
+ 2
Output hello! world! spam! eggs! It's because for loop in words .
6th Aug 2020, 4:14 AM
Sâñtôsh
Sâñtôsh - avatar
+ 1
Thank you very much!
6th Aug 2020, 4:34 AM
Adam Haller
Adam Haller - avatar