The syntax of a for loop is...
for number in range(4):
print("Hello")
will print
Hello
Hello
Hello
Hello
The program loops through the print("Hello") line 4 times. If we changed the number to 10 it would print 10 Hellos down the screen.
Computers start counting at zero
for number in range(8):
print(number)
print("Done")
This will produce
0
1
2
3
4
5
6
7
Done
This works like this:
range(8) produces a list containing 8 numbers, because computers start counting at 0 these numbers are 0, 1, 2, 3, 4, 5, 6, 7
The for loop creates a variable called number and sets it equal to each number in the list until it has gone through the whole list
Every time number gets a new value the program does the indented code, in this case it prints the value of number
Once number has been each thing in the list the program runs the next bit of code after the indented section which is printing the word Done in this program.
The range( ) function can take 3 arguments: start number, end number, and how much to go up each time.
The last number is optional; Python will assume you want the list to go up in 1s if you don't write anything.
range(6) will produce the list 0, 1, 2, 3, 4, 5 - Six numbers, starts at 0 and ends when it gets to 6, so 6 is not in the list
range(5, 12) will produce 5, 6, 7, 8, 9, 10, 11 - Starts at 5 and ends when it gets to 12, so 12 is not on the list
range(5, 20, 3) will produce 5, 8, 11, 14, 17 - Starts at 5, goes up in 3s, stops when it gets a value of 20 or more. Note: range(5, 19, 3) will also produce 5, 8, 11, 14, 17 as the next number will be 20 which is more than the end value of 19. range(5, 18, 3) will produce the same sequence too.
If you want the numbers to go down you need to use negative numbers at the end, like these:
range(10, 0, -1) will produce 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
range(20, 5, -2) will produce 20, 18, 16, 14, 12, 10, 8, 6 - Starts at 20, ends when it gets to 5 or less, goes down in 2s
**NB The numbers in the range function must be integers
A word or a list can also be used to control a for loop.
for letter in "Dog":
print(letter)
Will produce
D
o
g
The for loop creates the variable letter, then makes it equal to each letter in the word 'Dog', looping through the print line for each letter.
**NB The computer doesn't know that letter is actually a letter in a word. We could have used banana for the variable name like this:
for banana in "Dog":
print(banana)
and it would have worked in exactly the same way, but it will make more sense to someone reading your code if you use letter rather than banana !!
You can make your own list by using the square brackets [ ] and separating each thing on the list with a comma. eg
houses = ["Kauri", "Matai", "Rimu", "Totara"]
We can then use this list just like the lists produced by the range( ) function or the letters in a text string.
houses = ["Kauri", "Matai", "Rimu", "Totara"]
for house in houses:
print(house)
will produce
Kauri
Matai
Rimu
Totara
**NB It is usual to use plural names for lists
For Loops - Intro video
For 2 - How range works
For 3 - Using Text and Lists to control a for loop