June 2015
Beginner
348 pages
8h 44m
English
We can use the for loop in the following ways:
>>> food = ['ham', 'egg', 'spam'] >>> for snack in food: ... print(snack) ... ham egg spam
range() or xrange() functions. The latter function is slightly more efficient in certain cases. Loop over the numbers 1-9 with a step of 2 as follows:>>> for i in range(1, 9, 2): ... print(i) ... 1 3 5 7
range() function are optional with default values of 1. We can also prematurely end a loop. Loop over the numbers 0-9 and break out of ...Read now
Unlock full access