+ 1

What is the purpose of ' i ' in this code?

https://code.sololearn.com/c1s4DzMYwBg3/?ref=app

18th Dec 2022, 11:38 AM
YMHirose
YMHirose - avatar
5 Answers
+ 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
18th Dec 2022, 11:52 AM
iTech
iTech - avatar
+ 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, ...
18th Dec 2022, 7:55 PM
Lothar
Lothar - avatar
+ 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.
18th Dec 2022, 11:54 AM
Ausgrindtube
Ausgrindtube - avatar
+ 4
i is an index for accessing items of the list.
18th Dec 2022, 11:42 AM
JaScript
JaScript - avatar