[Tutor] For Loop question
Cédric Lucantis
omer at no-log.org
Thu Jun 26 15:49:02 CEST 2008
Le Thursday 26 June 2008 15:27:05 Danny Laya, vous avez écrit :
> Hi I'm learning FOR loop now, very easy too learn. But I get confused to
> understand this code : myList = [1,2,3,4]
> for index in range(len(myList)):
> myList[index] += 1
> print myList
>
> And the response is:
> [2, 3, 4, 5]
>
> Can you explain me as a newbie, how that code works ??
The 'for in' construct loops over the items of a sequence, and the range
function creates a sequence (list) of integers. It is described here:
http://docs.python.org/lib/built-in-funcs.html
so your code is equivalent to:
index = 0
while index < len(myList) :
myList[index] += 1
index += 1
--
Cédric Lucantis
More information about the Tutor
mailing list