+ 1

for word in words: What is "word" in this example?

How can Python know what this variable means? We didn't assigned its value.

12th Dec 2015, 7:43 PM
orca
3 Answers
+ 3
for the 1st iteration word=words[0] for the 2nd iteration word=words[1] and so on it's nothing but a temporary variable
22nd Jan 2016, 11:53 AM
Abhishek Biswas
Abhishek Biswas - avatar
+ 1
In python, while using the for loop, it automatically creates the variable for you and assigns every element in the array to it. In this case, word is a random variable chosen. It could have been anything like, tuna, potato, apple, and still the result would have been the same.
27th Jan 2016, 9:55 AM
kevin kibet
kevin kibet - avatar
0
it's arbitrary. they could have used any variable name to hold the current value of the list. Similar syntax is used in other scripting language such as bash.
8th Feb 2016, 4:36 AM
Matt Thompson
Matt Thompson - avatar