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?
5/17/2020 1:45:04 AMBrian Gomez
5 AnswersNew Answer
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
Brian Gomez The beauty of python is that you can assign variables on the fly. In other languages you would have to assign a variable and then populate it. 'word' (or 'abc') is simply a variable that is being assigned to accommodate each list item as you iterate through it. when you get to list comprehension it becomes even more fun... --- words = ["eggs", "bacon", "toast", "hashbrowns", "sausage gravy"] print("\n".join([abc+"!" for abc in words])) --- I am 5 months in learning python. Trust me, it will all make sense once you complete the tutorial and write some code! Happy Coding! 👍
Brian Gomez when you use "for" it is common to iterate thru an object (list, string, tuple, dictionary, etc), and 'in' is the keyword to check 'in' the object. 'word' (or 'abc') is a temporary variable that places a copy of the objects elements as it is being iterated thru the code block. the temporary variable is only available while the 'for' block is executing, and allows you to make copies of the current object to manipulate or assign to another variable to use outside the block. the best way to understand what it is doing is run a piece of code in an IDE that can show variables dynamically to see how and when objects are created and modified. Most IDEs can do this. I used Thonny.org IDE when I first started learning python. I use VSCode now. They both have a 'debug' mode with this capability. 😉👍
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
So IN can also check if the item is in a list AND in a way assign values to a variable?