+ 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 + "!")
14 Respuestas
+ 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)
+ 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]....)
+ 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"!"
+ 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.
+ 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?
+ 1
for _ in words
also good
0
definition of for loop?
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
0
It's a variable defined in the for loop.
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
0
You can choose any name for variable between "for" and "in".
You can use that name in body of loop or not use.
0
O'zbeklar bo'sa menga yozsin
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.
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).