Hello, I think this is a very simple question but I am bit stumped around my understanding of for loops. In the following code:
apps _data = [[‘Call of Duty: Zombies’, 5.0], [‘Facebook’, 0.0], [‘Instagram’, 0.0], [‘Temple Run’, 0.0]]
for app in apps_data: price = app[1] if price == 0.0: app.append('free') if price != 0.0: app.append('non-free')
print(apps_data)
My understanding is that app is the iteration variable, it takes on the value of the first element of apps_data then runs the rest of the loop. In this case we indicate which element is price then tell the computer that if price is equal to 0 append “free” to app (which is the first element of apps_data right now) and if price does not equal 0 append “non-free” to app. Then the loop resets, and app takes on the next element of apps_data.
To me this code would append free to the variable app which during the first iteration would be the first element of apps_data. Then when the first iteration of the loop finishes, app is then replaced with the second list element of apps_data, until all the iterations are done. Meaning if we print apps_data at the end nothing will have changed and if we print app we will get the final element of apps_data with free appended to the list. However when we print apps_data, each element of apps_data has free or non-free appended to it. Does this mean that whatever changes you do to your iteration variable are then reflected into the iterable variable? The only reason I haven’t seen this happen yet is because we haven’t made changes to the iterable variable, just ran counts/comparison operators?
So really what is happening with a for loop is that we take the iteration variable and use it to store the first element of the iterable variable, at the end of the iteration whatever changes we made to the iteration variable are then put back into the iterable variable. Is this understanding correct? Sorry if this is too much text and I didn’t include code properly. This is my first post.
Thanks