+ 19

For Loops

Could anyone explain to me how does computer know the variable 'word' here? Like, we can put here anything? And still, how does it know what word is? It's never defined before so I have no idea why! words = ["hello", "world", "spam", "eggs"] for word in words: print(word + "!")

12th Apr 2018, 1:51 PM
chionae
chionae - avatar
14 Respostas
+ 28
so the thing is: the for loop will iterate (loop through) the whole array 'words'. for every word that you have stored in 'words' it will load it to the variable 'word' and print it, as you specified. so, here we go: for <- you tell the interpreter that it is a loop word <- WHEREto store variables (from an array or other thing, specified as fours term (wait)) in <- tells from which array you take items words <- name of that array for <name of variable> in <array name> for loop will do the specified thing ( print in your example) with EVERY thing that is stored in the array ('words' in your example)
12th Apr 2018, 1:57 PM
Paul
+ 28
it gets auto-defined, it is supposed to iterate through words, thus, there are secret assignments on the background (word = words[0] then : word = words[1]....)
12th Apr 2018, 1:53 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 9
This is a for loop and you can replace word by any word you want as like: for n in words: print(n+"!") word here is used in arbitrary choice of terms and the entire meaning of the for loop is: for element in words print this element with exclamation mark"!"
12th Apr 2018, 4:10 PM
HBhZ_C
HBhZ_C - avatar
+ 3
Actually computer doesn't know the meaning of word here. It takes the word as a variable. You can use anything in place of word such as I, j,k any variable. Here the programmer uses 'word' for better code readability.
4th Dec 2021, 4:04 PM
Prithviraj Kundu
Prithviraj Kundu - avatar
+ 2
I didn't get this(for,while,loop variables,range).I am stuck at fizzbuzz dont now how can I write solo after every 3 interval and learn after every 5 interval. Can someone pls give me summary of all this and explain how to do this?
22nd Feb 2022, 3:34 PM
Yug
Yug - avatar
+ 1
for _ in words also good
19th Feb 2022, 4:49 PM
stpot
0
definition of for loop?
13th Apr 2018, 12:18 PM
Atul Pradhan
Atul Pradhan - avatar
0
deoends of the loop tho. in c++ for loop is defined slightly different that the one here, fut lets focus on the given example. For loop will iterate through every item in an array and you can do some things to each of them. that is for loop, nothing very special
13th Apr 2018, 1:59 PM
Paul
0
It's a variable defined in the for loop.
29th Jul 2021, 6:47 PM
Bobbersnucker
Bobbersnucker - avatar
0
It's easyto understanding, he give a value already given by system and any word, like exemple here (word) change the previous value on language by this string and go on
23rd Aug 2021, 9:33 AM
Mr. anrsaad
Mr. anrsaad - avatar
0
You can choose any name for variable between "for" and "in". You can use that name in body of loop or not use.
6th Nov 2021, 8:01 AM
stpot
0
O'zbeklar bo'sa menga yozsin
19th Feb 2022, 4:22 PM
Abduraimxon. Nurullayev.
Abduraimxon. Nurullayev. - avatar
0
'word' is just for easy readability, it is up to you to define the variable. It could be cow or goat, i.e. for cow in words: .... It would still work.
23rd Aug 2022, 8:44 AM
Asa'ah Ndangoh
Asa'ah Ndangoh - avatar
0
“word” in the line; for word in words:, is when the variable word is defined, and it is the new subject of each iteration. In this case the value of word is “hello”, “world”, “spam”, and “eggs”; changing each time the code iterates through the line; for word in words:. The variable “word” is assigned each item in the referenced variable. For this operation, “word” equals each word in words, words is the already defined variable (which is a list).
1st Nov 2022, 11:38 AM
Daniel T Rials
Daniel T Rials - avatar