Welcome to the Treehouse Community
Looking to learn something new?
In For loops ... where do the parameters come from - like item in inventory ... where does "item" come from?
... hopefully my English is good enough to describe my question ... in the REPL ... Craig writes ... item in inventory ... sure the list contains items, but how can it be processed ... I did not understand this before, but there it was attendee in attendees ... and the string attendee is part of attendees, or wish is part of wishes, but item is not in inventory ... hope this makes sense ... I already didn't know how to formulate the question, in order to be understood ... maybe I should rewatch the video ... helped several times ...
for loops in any language loops in a similar format. It contains 3 constructs...
- end_parameter(loop runs till this condition is true)
for i in range(0, 10): print(i) # will give all the numbers from o to 9. Here staring was 0, end was 10(anything less than 10 is fine) and finally incrementer is 1(by default)
for i in range(0, 10, 3): print(i) # will give all the numbers 0, 3, 6, 9. Here staring was 0, end was 10(anything less than 10 is fine) and finally incrementer is 3(last parameter)
For a list... well, range(0, x) will return a range class but you can imagine it like a list.
for item in [10, 2, 3, 4, 5]: print(i) #Will return 10, 2, 3, 4, 5. i.e, elements of list one by one. here start is 10, end is 5, and incrementer is not a number but an iterator that is incremeting by 1.
So when you say..
for item in inventory: # item refers to an element in the inventory
you might be getting confused with:
if "item" in "inventory": # or if "attendee" in "attendees":
Here it is in is checking if first string("items" or "attendee") is present in second string.
in checks for membership.
so in case of for loops it checks for elements in list.
and for strings as well, it does the same operation. Since strings can be treated as list("item"="i", "item"="t","item"="e", "item"="m").
Hope this helps.
thank you for your answer Krishna ... the confusion derived from ... I got stuck ... because I thought, it must have been declared somewhere before ... and I just didn't get it ... but it is declared right then ... but your explanation is very good ... especially, when you mentioned the "i" ... which also isn't "declared" at some point before ... the "i" cleared it up even m ore ... thank you a lot ... Jan.