For loop in Python

Keywords: for loop, counting loop, loops

for loop Python programming

The for loop in Python is used when you want an operation to be repeated a specific number of times. Thanks to the for loop, you do not have to code the same thing several times because you can reuse the same code several times instead.

What is a for loop in Python?

The for loop in Python is used to iterate (repeat) part of the program several times. If the number of iterations (repetitions) is a fixed number, it is recommended to use the for loop.

The image below a flow chart of how the for loop works. If the condition is met (Yes) then the operation is performed, if the condition is not met (No) then the program moves on.

After the operation is performed, the given initial value is increased and the loop then repeats the process and returns and checks if the condition is met.

How the for loop in Python work?

The for loop works in a similar way as the while loop that we described in the previous article. If we illustrate the for loop using a flow chart, we get:

For loop Python Programming

What the figure shows is that:

  1. The for loop starts by checking if the condition is true. In that case, the loop will execute the code block within the loop.
  2. When all operations are executed, the loop increases the initial value, also called the counter, and the program returns to the beginning of the for loop again.
  3. If the condition is still true after the initial value has been updated, the for loop executes the code block again. This procedure continues as long as the condition is true.
  4. Finally, when the condition becomes false, the program moves on. 

Create a for loop in Python

The for loop is easily declare through the reserved word for. The for loop in the simplest form is done by specifying how many times you want the loop to be executed. This can be done with the command into in range (value):

  • This means that the loop will be executed as many times as the value indicates.
  • The value of the variable i will then change automatically after each iteration between 0 to the value – 1. See example below.

Syntax: Declare for loop in Python

for i in range(value):
    code executed within the for loop

Example: For loop in Python

Let’s take a simple example of how we can use the for loop to perform an operation a given number of times.

  • The for loop repeats the operation as long as “i” is in the specified range
  • The program prints the text “Counter:” and the value on the variable “i
  • In this example, the process will be executed 5 times, since it is the value specified in range ().
for i in range(5): 
    print("Counter:", i)

The good thing about the for loop is that the value of the variable in, automatically increases after each iteration.

The result in this case becomes:

Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4

So we have in a simple and fast way written a program that prints a value 5 times with the help of a loop, which is much easier than printing five times (although the result would be the same). But what if you wanted to print a value 100 times? Or maybe 1000?

For more technical and specific information about the for loop we recommend the Python docs website

Summary: For loop in Python

The for loop in Python is a so-called counting loop that repeats a code sequence a predetermined number of times. Hence, the for loop is best suited when you know the number of iterations that the loop will need to do

Syntax: Declare for loop in Python

for i in range(value):
    code executed within the for loop