Loops in Python (with Examples)

The word "loop" refers to a shape produced by a curve that bends around, means that it runs continuously when in the loop. In the same way, a loop in Python is used to execute a block of code multiple times.

Types of Loops in Python

There are three types of loops available in Python, including nested loops. Here is the list of all three loops:

Python Loop Examples

Since both "for" and "while" loops are described in detail in separate tutorials, therefore, this article only includes the basics of these loops. For details, you can refer to the separate tutorial on it. Also, the next tutorial is about "for" loops. For now, let's take an example that illustrates loops in Python:

for i in range(5):
    print(i)

The output produced by the above loop program in Python is shown in the snapshot given below:

python loops

As the above program uses the for loop, let's create the same program using the while loop:

i = 0
while i<5:
    print(i)
    i = i+1

This program produces exactly the same output as the previous program. As you can see, the main task of both loops in Python is to execute a block of code multiple times, or the required number of times. That is, in the first program, the following code is a single statement:

print(i)

gets executed five times. Whereas, in the second program, the following block of code contains multiple statements:

print(i)
i = i+1

also gets executed 5 times. That is, the program flow continuously goes into the body of the while loop, with a new value of i (the loop variable) each time, until the condition "i<5" evaluates to false.

Nested Loop in Python

Here is an example program that demonstrates the concept and use of nested loops in Python:

for i in range(5):
    for j in range(3):
        print(i, "\t", j)
    print()

The output produced by the above program is:

loops in python

The above program can also be written like this to produce a more understandable output:

for i in range(5):
    for j in range(3):
        print("i =", i, "\t", "j =", j)
    print()

Here is the output produced by the above program, this time:

python loops example program

Note: The range() function generates a sequence of numbers. If only one parameter is provided to it, then it will generate a sequence of numbers starting with 0 and ending with one less than the parameter's value. For example, range (5) returns or gives 0, 1, 2, 3, 4.

So, if you replace range() with the list of numbers it gives you back, the above program becomes:

for i in 0, 1, 2, 3, 4:
    for j in 0, 1, 2:
        print(i, "\t", j)
    print()

The program still produces the same output. You can check it out for yourself.

Therefore, instead of wasting time writing these sequences, we need range(). Therefore, most of the time, we use this function to iterate through a loop in Python. We can also iterate over strings, lists, etc. Iterating these objects is described in its own separate tutorial.

The same program of nested loops can also be created using the while loop, as shown in the program given below:

i = 0
while i<5:
    j = 0
    while j<3:
        print(i, "\t", j)
        j = j+1
    print()
    i = i+1

produces exactly the same output as the previous program.

More Examples

Python Online Test


« Previous Tutorial Next Tutorial »