Loops | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Loops

words = ["hello", "world", "spam", "eggs"] for word in words: print(word + "!") This is the for loop tutorial. I dont understand how it is possible to check if 'word' is in the list when its not? What is the purpose of 'word'? What dies it do exactly? Is it a variable that is already defined?

17th May 2020, 1:45 AM
Brian Gomez
Brian Gomez - avatar
3 Answers
+ 2
iteration - they are using for word in words but it can be anything like for abc in words... iteration repetition of a mathematical or computational procedure applied to the result of a previous application, typically as a means of obtaining successively closer approximations to the solution of a problem. https://code.sololearn.com/cnoL49xcKYg4/?ref=app
17th May 2020, 2:03 AM
BroFar
BroFar - avatar
0
Have you checked the Python course? The for loop is a loop that iterates through an iterable An iterable can be any collection in Python (a list for example), a string or a range The for loop moves through each element in the iterable (basically you want to do something with each element) It goes through the 'hello' element in the list given and stores it in the variable word which is to be printed then it finishes the turn at the end of the block and moves to the second element 'world' and the value of the variable word is then replaced with 'world' to be printed and so on Word is just a name, it can be anything for item in words: # do something item has the same rule which is being a temporary variable that stores each element at a time Notice that the variable word is only accessable through the loop and is not related to anything outside even if there is a variable outside the loop with the same name
17th May 2020, 2:06 AM
Ali Abdelhady
Ali Abdelhady - avatar
0
So IN can also check if the item is in a list AND in a way assign values to a variable?
18th May 2020, 12:45 AM
Brian Gomez
Brian Gomez - avatar