Discover Loop Iteration in Python | Lesson 6

Last updated on January 12th, 2026 at 11:04 am

Grammarly Writing Support

Loops are a crucial part of programming, and understanding loop iteration is essential for becoming an expert programmer. For clarity, loops allow you to execute a block of code repeatedly without having to write the same code multiple times.

If you have not used loops before, be sure to explore mastering loops in Python.

Keep reading to learn:

  • What a loop iteration is; and
  • Loop iteration in for and while loops.

What is a loop iteration?

A loop iteration refers to the process of executing a block of code once within a loop. In a loop, the code iteration is designed to run multiple times. When the iteration is repeated, we call it looping. Each time the block runs, it is referred to as an iteration.

How loop iteration works in a while loop

As long as a condition remains true, while loops will continue to execute the code. Here is a basic syntax of a while loop.

1. count = 1 

2. while count<= 5:
3.   print (count)
4.   count += 1

So here is what is happening in this program:

  1. In this example, at the beginning of the program, count has the value 1.
  2. The second line is the beginning of the loop and holds the condition. The condition checks if the value contained in the variable count is less than or equal to 5. Since the value is 1, the condition is true.
  3. So at line 3, the value 1 will be printed.
  4. In the 4th line, 1 is being added to whatever is contained in the variable count: 1 + 1 = 2. As such count is now 2.
  5. The second time the loop runs, it will simply go to line 2 to check the condition to see if count is still less than 5. Since we have established that count contains the value 2, we know count is less than or equal to 5.
  6. In the next line, the value contained in count, 2 will be printed.
  7. In the next line, 1 is added to the value contained in count: 1 + 2 = 3. So count now has the value 3.
  8. The program jumps back to line 2, where it checks whether the condition is still true. Since count holds 3, it is true.
  9. At line 3, the value contained in count 3 will be printed.
  10. At line 4, 1 will be added to the count. 1 + 3 = 4. So count holds 4.
  11. Next, the program will loop up. It will check to see if the count is still less than or equal to 5. Yes, it is.
  12. At line 3, the value contained in count, 4 is printed.
  13. At line 4, 1 is added to the value contained in count. 1 + 4 = 5. Count now contains 5.
  14. The program will jump up again to line 2 so the condition can be checked. Is the value contained in count, 5 less than or equal to 5? Yes, it is equal to 5, so this will evaluate to true.
  15. In the next line count, 5 will be printed.
  16. In the next line, 1 will be added to the value contained in count: 1 + 5 = 6. So count now contains 6.
  17. The program will return to line 2 to recheck the condition. This time, because count contains 6, it will evaluate to false. As such, the loop will come to an end.

This is an example of what happens when a while loop is executing its looping process.

User-controlled loop iteration – while loop

There are times when we write programs that require a user to enter a specific input to terminate the program. The program runs similarly to the one above, but on each iteration, it checks to see if the user has entered the value to terminate the loop.

As long as the value is not entered, the loop will continue to run. Below is an example of such a while loop.

password =""
while password != "secret":
  password = input ("Enter your password:  \n")
print("Access granted!")

Loop iteration using a for loop

The loop iteration for the for loop is different in that it is not explicitly governed by a condition. So what controls a for loop? Let’s look at an example.

colours = "red"
for colour in colours:
  print(colour)

Let’s take a look at the printout. This program prints:

r
e
d

The values that govern how many times the program will iterate is dealt with in line two. The variable colours contain red. The line for colour in colours says that the program should iterate according to the number of items contained in colours.

Let’s change the output in this program so you can understand better.

colours = "red"
for colour in colours:
  print("Sue")

If you run this code, the output is:

Sue
Sue
Sue

There you have it, the number of iterations is governed by the number of values in the variable colours. Essentially, while there is no counter, the for loop will run until there are no more items in colours.

Before You Go

A loop iteration is each execution of the code within a loop. We explored how to do this in both a simple while loop and a for loop. To become an expert programmer, it is extremely important for you to master loops. We will delve further into this in the next lesson on mastering for loops.

Additionally, if you have any questions or comments, please leave them in the section below, and we will respond as soon as possible.

Leave a Comment