0

Why do we say( for word in words) "For loops"

Can the "word" part be assigned to any word?

10th Aug 2020, 5:53 PM
DeadEye UNT
DeadEye UNT - avatar
7 odpowiedzi
+ 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.
10th Aug 2020, 6:46 PM
Slick
Slick - avatar
+ 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.
10th Aug 2020, 5:57 PM
Slick
Slick - avatar
+ 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
10th Aug 2020, 6:23 PM
Jayakrishna 🇮🇳
+ 2
Thank you for taking your time to help me 👍
10th Aug 2020, 7:06 PM
DeadEye UNT
DeadEye UNT - avatar
+ 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
12th Aug 2020, 4:38 AM
Artem 🇺🇦
Artem 🇺🇦 - avatar
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"
10th Aug 2020, 6:33 PM
DeadEye UNT
DeadEye UNT - avatar
0
I would love to read more questions about this
10th Aug 2020, 6:35 PM
DeadEye UNT
DeadEye UNT - avatar