The for loop is one of the primitive loop commands in Python that is usually used to iterate over sequences such as lists, tuples, and strings. The general form of a for loop can be written as:
Example 1 Create a simple for loop that will show the numbers from 0 to 10 (not including 10).
Solution: This program is very simple and is practically done in two lines of code. The first line is the definition of the for loop that will iterate over integers from 0 to 10 (not including 10).
Solution: The code for this example can be defined in two lines. In the first line, we will define the for a loop.
Solution: The procedure is the same as in previous examples. The first line is the for loop definition and the second line is the line that will show the current iteration. The entire code is given below.
Example 4 Create for loop to iterate backwards from 20 to 0.
Solution: In this example we will also use the range function. The first number in the range function is 20, the second is 0 and the third number is -1. The entire code is similar to previous examples.
We can use the for loop to print out each character of a string. Example 5 Create a for loop that will print each character of a string "Transformers".
Solution: The procedure is similar as in previous example. However, in this example, we will not use the range function. The code consists of three lines. First, we will define the string and assign it to a variable word.
Example 6 Show list elements using for loop. The list is [1, "ten", 3, "String"].
Solution: First step we have to define a list and assign it to a variable. Then create for loop that will iterate and show the list elements.
for val in sequence:In the previous code block, the val is the variable that takes the value of the item inside the sequence on each iteration. The for loop is executed until the last item in the sequence is reached. In Example 1 the simple for loop is created.
indented statements
Example 1 Create a simple for loop that will show the numbers from 0 to 10 (not including 10).
Solution: This program is very simple and is practically done in two lines of code. The first line is the definition of the for loop that will iterate over integers from 0 to 10 (not including 10).
for i in range(0,10,1):The number 1 is optional. If we typed range(0,10) the result would be the same. The second line will be indented since it will be executed in each iteration of the for a loop. This line will show as output the value of i (iteration number)
for i in range(0,10,1):Output:
print("i = ", i)
i = 0Example 2 Create for loop that will print every even number in range from 0 to 20 (including 20).
i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9
Solution: The code for this example can be defined in two lines. In the first line, we will define the for a loop.
for i in range(0,22,2):In previous code line the interesting part is range(0,22,2). The first and second numbers i.e. 0 and 22 represent the range. The last number (number 2) represents an iteration step. Since we want to print every second (even) number in a range from 0 the 22 the iteration step was set to 2. The second line of code will be used to show as output the iteration number of each for loop iteration. Note that the second line has to be indented i.e. inside the for loop body since it will be executed in each iteration of the for a loop.
print("i = ",i)The entire code of Example 2 is given below.
for i in range(0, 22, 2):Output:
print("i = ",i)
i = 0Example 3 Create for loop that will iterate from 0 to 50 with increment equal to 5.
i = 2
i = 4
i = 6
i = 8
i = 10
i = 12
i = 14
i = 16
i = 18
i = 20
Solution: The procedure is the same as in previous examples. The first line is the for loop definition and the second line is the line that will show the current iteration. The entire code is given below.
for i in range(0, 50, 5):Output:
print("i = ",i)
i = 0So far the for loop iterated in a range from 0 to 10 or 20. The question is can for loop be used to iterate backward?
i = 5
i = 10
i = 15
i = 20
i = 25
i = 30
i = 35
i = 40
i = 45
Example 4 Create for loop to iterate backwards from 20 to 0.
Solution: In this example we will also use the range function. The first number in the range function is 20, the second is 0 and the third number is -1. The entire code is similar to previous examples.
for i in range(20,0,-1):Output:
print("i = ",i)
i = 20
i = 19
i = 18
i = 17
i = 16
i = 15
i = 14
i = 13
i = 12
i = 11
i = 10
i = 9
i = 8
i = 7
i = 6
i = 5
i = 4
i = 3
i = 2
i = 1
We can use the for loop to print out each character of a string. Example 5 Create a for loop that will print each character of a string "Transformers".
Solution: The procedure is similar as in previous example. However, in this example, we will not use the range function. The code consists of three lines. First, we will define the string and assign it to a variable word.
word = "Transformers"The second and third line of code is the definition of the for loop and the for loop body.
for character in word:Output:
print("character = ", character)
character = TThe for loop can be used to show elements of a list.
character = r
character = a
character = n
character = s
character = f
character = o
character = r
character = m
character = e
character = r
character = s
Example 6 Show list elements using for loop. The list is [1, "ten", 3, "String"].
Solution: First step we have to define a list and assign it to a variable. Then create for loop that will iterate and show the list elements.
simpleList = [1, "ten", 3, "String"]Output:
for i in range(len(simpleList)):
print("simpleList = ",simpleList[i])
simpleList = 1
simpleList = ten
simpleList = 3
simpleList = String