+ 1
What is the purpose of ' i ' in this code?
5 Respostas
+ 4
In your case, i is an iteration variable and represents the elements in the list.
It's not the index!!!
If you sum the indexes of the element it should be 3 not 6 because index start with 0 such as l[0] = 1.
Explanation of python list slicing
https://www.geeksforgeeks.org/python-list-slicing/
Explanation of iteration, iterable and iterator
https://stackoverflow.com/questions/9884132/what-are-iterator-iterable-and-iteration
+ 8
YMHirose ,
in the current code:
> 'i' is NOT an index, and it is also NOT an iterable.
> 'i' is the name of a variable (loop variable), that gets values during loop iterations from the iterable 'list', one at a time.
(btw - we should not use the name 'list', since python has a built-in data structure with the same name)
> in the first loop iteration it gets the value 1, in the second lopp iterstion it gets the value 2, ...
+ 5
It's just "the name" of the variable, as JaScript mentioned. You could choose any other letter or even words as long as the words conform to naming conventions.
Change "i" to "numToAdd" everywhere in the code and see what happens.
+ 4
i is an index for accessing items of the list.