FOR loops allow programs to repeat a section of code for a set number of times.
If you know the number of times some code needs to repeat; a FOR loop is perfect.
This loop will display the value of 'i' and increase the value or 'i' until it has done it ten times.
As Python code:
for i in range(1, 10):
print(i)
Like the IF-ELSE you need a colon at the end of the line.
And all the lines of code that are to be repeated must be indented.
This will output:
for i in range(1, 10):
print(i)
1 2 3 4 5 6 7 8 9
range(1, 10) will start with 1 but will end before 10.
This will output:
for i in range(1, 10, 2):
print(i)
1 3 5 7 9
range(1, 10, 2) will start with 1 but will increase by 2 each time.
This will output:
for i in range(10, 1, -3):
print(i)
10 7 4
range(10, 1, -3) will start with 10 and will decrease by 3 each time.
This will output:
for letter in 'word':
print(letter)
w o r d
Steps through each character in the word 'word'.