0
Why do we say( for word in words) "For loops"
Can the "word" part be assigned to any word?
7 Respostas
+ 2
Look, i have a basket of things.
For each thing in the basket, Ill say "I found a thing!"
(if i have 3 things in my basket)
I found a thing!
I found a thing!
I found a thing!
My basket is still my basket!
Youre focusing too much on it, mabey thinking too much.
+ 6
yup, say you have a list of words.
words = ['hi', 'there', 'pi', 'bear']
# you CAN type (but dont know why)
for monkey in words:
print(monkey)
# and output would be
hi
there
pi
bear
youre basically assigning each item in the iterable a variable name on the fly, printing it, then automatically assigning that variable name to the next item and so on.
+ 4
' word ' is there a temporary variable name.. You can use any name..
for i in words :
for j in words :
for word in words :
for word1 in words :
All valid..
If you need assign to any other
ex:
wordfound = word
+ 2
Thank you for taking your time to help me 👍
+ 1
"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
0
Computer=['CPU', 'RAM', 'GPU']
So one would say
For parts in computer
print(parts)
CPU
RAM
GPU
So does that mean that "Computer" has changed to "parts" or is it still computer but parts is a temporary varaible so after you said that it stays computer or... All the code further down would be using the varaible "parts"
0
I would love to read more questions about this