15. Iterations


I truly do not understand range and the whole ‘i’ thing. Can someone explain to me why/how to use it?
I just don’t understand what it does. Lamens terms are much appreciated

n = [3, 5, 7]

def total(numbers):
  result = 0
  for i in range(len(numbers)):
    result = numbers[i] + result
  return result


look at this code:

n = [3, 5, 7]
for i in n:
    print i

i is defined in the loop, python will (in order) assign the values from the list to i, after a value is assigned to i, it executes the code within the for loop (in my example print i), once the loop is done, python will assign the next value from list to i, execute the code in the for loop again and so on until its through all the values in the list.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.