Struggling to understamd


#1

I'm getting the answers right but I dont understand how the for loop works. for example I dont understand this:
for item in list:
print item

for i in range(len(list)):
print list[i]

plz help me to understand


#2

The for loop iterates over a list. This list can be of values (the way range(5) gives you the values [0, 1, 2, 3, 4]) or a list of objects. Then, every "run" of the for body, it assigns a name (in your case, item) so you can work with it.

In your first example, the first time the for runs item will be the first element on the list. If list has more elements, the for's code will run again, but this time item will be the next element on the list. And so on untill it's done for all elements in the list.

In your second example it's exactly the same, it's just that instead of i being any kind of object, you know it's a number between 0 and len(list), so you can use it as an index to get a specific item from list.