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 _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor