For loops

Definition

In Python, a for loop can be used in two ways. It can either repeat a block of code a pre-defined number of times, or it can cycle each item in a list.

Warning

A range starts at 0 and doesn’t include the upper number. For example, range(5) would include the numbers 0, 1, 2, 3, 4.

Easy example

for i in range(5):
    print(i)
Show/Hide Output
0
1
2
3
4

Note

Programmers usually use i as the counter variable name. You can use different variable names for the counter, for example: for count in range(5):

Syntax - using range

for i in range(number):
    print('statement 1')      #put statements here
    print('statement 2')      #this statement is part of the loop

print('statement3')           #this statement isn't part of the loop

Examples

Example 1 - Output ‘hello’ four times

for i in range(4):
    print('Hello')
Show/Hide Output
Hello
Hello
Hello
Hello

Example 2 - Output from 1 to 10

for i in range(1, 11):
    print(i)
Show/Hide Output
1
2
3
4
5
6
7
8
9
10

Example 3 - Output from 0 to 5

for i in range(6):
    print(i)
Show/Hide Output
0
1
2
3
4
5

Example 4 - Output every other number from 1 to 10

for i in range(1, 11, 2):
    print(i)
Show/Hide Output
1
3
5
7
9

Example 5 - Output from 10 to 0

for i in range(10, -1, -1):
    print(i)
Show/Hide Output
10
9
8
7
6
5
4
3
2
1
0

Example 6 - Loop with concatenation (joining strings together)

for i in range(10):
    print('The current number is: ' + str(i))
Show/Hide Output
The current number is: 0
The current number is: 1
The current number is: 2
The current number is: 3
The current number is: 4
The current number is: 5
The current number is: 6
The current number is: 7
The current number is: 8
The current number is: 9

Example 7 - Loop through a string

secretWord = 'This is a secret'

for letter in secretWord:
    print(letter)
Show/Hide Output
T
h
i
s

i
s

a

s
e
c
r
e
t

Example 8 - Loop through a list (array)

popularNames = ['Emma', 'Liam', 'Olivia', 'Mason', 'Ava', 'Lucas']

for name in popularNames:
    print(name + ' is a popular name.')
Show/Hide Output
Emma is a popular name.
Liam is a popular name.
Olivia is a popular name.
Mason is a popular name.
Ava is a popular name.
Lucas is a popular name.

Example 9 - Nested loop - times tables

maxTimesTable = 3
numberOfRows = 10

for i in range(1, maxTimesTable + 1):
    for j in range(1, numberOfRows + 1):
        answer = i * j
        print(str(i) + '*' + str(j) + ' = ' + str(answer))

    print('----------')
Show/Hide Output
1*1 = 1
1*2 = 2
1*3 = 3
1*4 = 4
1*5 = 5
1*6 = 6
1*7 = 7
1*8 = 8
1*9 = 9
1*10 = 10
----------
2*1 = 2
2*2 = 4
2*3 = 6
2*4 = 8
2*5 = 10
2*6 = 12
2*7 = 14
2*8 = 16
2*9 = 18
2*10 = 20
----------
3*1 = 3
3*2 = 6
3*3 = 9
3*4 = 12
3*5 = 15
3*6 = 18
3*7 = 21
3*8 = 24
3*9 = 27
3*10 = 30
----------

Key points

Note

Loops can be nested. This means that a for loop is placed inside another for loop.

Hint

The variable name i is often used for a counter name. For nested loops, i, j, k are often used.

Warning

All statements in the loop must be tabbed over to the exact same amount if the are to be part of the loop.

Note

A for loop is one way of performing iteration (looping). The other way is to use a while loop. For loops are also known as count-controlled loops/iteration.

Note

Python allows you to repeat a string without using a loop. For example, print('Hello\n' * 5) will print the string Hello five times on the screen.