Loops in Python | Types, Example

In this tutorial, we will learn about loops in Python with the help of an example.

A loop in Python is a control structure that repeatedly executes a statement or a group of statements until a specific condition is true.

In other words, a loop is a control flow statement that allows the repetitive execution of a statement or group of statements repeatedly until a termination condition met. It facilitates the complicated execution paths easy.

Looping is also called a repetition structure in Python. The block of code can execute many times from zero to infinite number depending on the need.

One complete execution of all statements within a loop is called iteration in Python.

Let’s understand a loop statement in Python with the help of an example.

Python Loops Example


When we write a program in Python, the interpreter executes it sequentially. The statement which appears first in the sequence, Python executes it first, then the next statement, and so on till the last statement of the program.

Many times, we need to execute the same block of code in a program several times. For example, suppose that we need to print the string
‘Hello world’ a hundred times in a Python program.

It would be a tedious and pretty boring task to write the following statement a hundred times like this:

# Python program to print a statement 100 times. It would be a tedious and boring task.
print('Hello world')
print('Hello world')
print('Hello world')
. . . 
. . .
print('Hello world')

So, how will you make this tedious and boring task easy?

To make this task easy, Python programming language provides a powerful feature, or control structure, called loops. This loop controls how many times has to be executed a statement or the same bit of code repeatedly.


A loop simply tells the Python interpreter to execute a bit of code multiple times if the condition is true and exits the loop when the condition becomes false.

In the above example, we can use the loop statement to print a statement hundred times instead of writing the code a hundred times to print the same statement. The sample code to print a statement hundred times is as:

# This Python code uses a while loop to print a statement hundred times.
count = 0
while count < 100:
    print('Hello world')
    count += 1
print('Loop finished')

In the above code, initially, the variable count set to 0. The loop evaluates whether the expression count < 100 is true. If so, it executes the block or body of loop to print the statement “Hello world” and then it adds 1 to the value of count.

It repeatedly executes the block of statement within the loop until the condition count < 100 becomes false. When the condition count < 100 is false (i.e. when count reaches 100), the loop ends.

After execution of loop statement, the control comes outside from the loop and goes to execute the next statement following the loop and print the message ‘Loop finished’.


Thus, we can say that looping is a technique that executes the same bit of code over and over again.

Loop Control Structure in Python


A block of code runs sequentially in the loop until a specific condition for the termination of the loop met. In Python, a loop control structure comprises two parts:

  • Control statement
  • Body of the loop

The function of a control statement is to test or evaluate a given condition, also called boolean expression, before the execution of body of the loop. If the given condition satisfies, then the body of loop will execute.

As long as the test condition evaluates to true, the statements in the loop body continue to execute. If not satisfied, then the body of loop will not execute. This type of loop is called an entry-controlled loop.

The general flowchart diagram for loops in Python is as below:

General flowchart diagram of loops in Python

In all types of entry-controlled loops, there is a control variable called loop counter. You must initialize it with an initial value. The increment or decrement of the control variable modifies each time the iteration of loop occurs. The loop condition or expression determines the continuation of loop body.

Steps to Process Loops in Python


Basically, there are four steps to process loops in Python. They are as:

  • Setting and initialization of a control variable.
  • Execution of statements sequentially in the loop body.
  • Evaluation of a specified condition for the execution of statements inside the loop body.
  • Incrementing or decrementing of the counter.

Types of Loops (Iterations) in Python


Python programming language supports four types of loops to handle the loop operations. They are:

  • for loop
  • while loop
  • nested loops
  • infinite loops

The while loop is an entry-controlled loop statement that executes a statement or a group of statements inside the loop body repeatedly as long as the conditional expression evaluates to true.

It evaluates the conditional expression each time it executes the loop body and exits the loop when the conditional expression becomes false.

A for loop is an entry-controlled loop statement that executes a statement or a group of statements a certain number of times.

When we place a loop inside another, then it is called nested loops in Python. It allows us to loop over two variables. The outside loop is an outer loop, while the inside loop is an inner loop.

An infinite loop is a loop statement that repeats forever. We also known it as endless loop because the specified condition is always true and the loop body executes repeatedly as long as the program is running, and never ends.

We will learn about the definition, features and applications of each of these loops in the further tutorials one by one with the help of example programs.


In this tutorial, you have learned about different types of loops in Python with the help of example. Hope that you will have understood the basic points of loop and its general flowchart diagram.

In the next tutorial, we will learn about a simplest while loop statement in Python with the help of various example programs.
Thanks for reading!!!

⇐ Prev Next ⇒

Please share your love