Python:For Tutorial

From Progzoo
Jump to: navigation, search
<< Boxes Tutorial

You can use the for loop to make a section of code repeat many times.

Program Code Output
for i in range(10):
  print i








0
1
2
3
4
5
6
7
8
9

The line print i happens 10 times.

In python the for loop will go over each item in a list.

for i in any list
The range() function can be used to create a list of numbers, in this example the list contains the numbers 0 to 9.
range(10)
The loop goes through the list in order and executes the code in the body:
print