Loops in Python

Python statements are executed sequentially in any code, however, sometimes we wish to iterate a particular piece of code repeatedly. To attain this we use, Loops in Python.  The Loops in Python will allow us to execute a group of statements repetitively.

Loops are always accompanied by a condition, with the help of this condition we run the loop whenever the condition is true and break the loop or come out the loop whenever the condition is false. This acts as an important component in designing the loops according to our purpose.

The flow chart of Loops in Python is shown below:

The main motive of this condition is to bring a definitive end to our loop, i.e. to come out the loop after repeating n number of times. If the condition is always true, we end up with an infinite loop that goes on running continuously.

Types of Loops in Python

We have two primitive types of Loops in Python:

  1. For loop
  2. While loop  

Let’s look into each type.

FOR Loop

The For loop is used to iterate a particular block of code several times. It is mainly used for traversing over sequences or data structures like list, tuple, dictionary, etc.

Syntax:

foriterating_var insequence:
    statement(s)

Here, iterating_varis the iterating variable that selects the first item in the sequence, executes the given statements in the loop, then moves on to the next item in the sequence. This goes on until the last item is selected and the statements of the loop are executed. Thereafter, we come out of the loop and the remaining statements of the code are executed.

It is better to go with for loop when we know the number of iterations in advance.

Let’s have a look at a few examples.

#Example of for loop
num = range(10)
for i in num:
print(i)
print("Out of the loop")

Output

Here, we have used the range() function which gives us the sequence of numbers starting from 0 by default. The argument 10 in the function means that we want 10 numbers as output and therefore we get values from 0 to 9.

Therefore, with the help of this range()function we created a sequence named numand our iterating variable here is i.

colors = ['red','blue','green','yellow','white','black']  #A list of colors
for col in colors:
print(col)

Output

Here, we can see that we come out of the loop after the list is exhaustive.

WHILE Loop

The While loop in Python is used to execute a group of statements within the loop until the given condition is satisfied.  When the condition becomes false, we come out of the while loop and the rest of the lines in the code are executed.

Syntax:

whileexpression:
    statement(s)

It is adequate to use while loop when we are not aware of the number of iterations in advance.

Let’s have a look at a an example.

#Example of while loop
n = 7
while n>0:
print(n)
n = n-1
print("Out of the loop")

Output

Therefore, here we can see that we came out of the loop when the condition of n>0was not satisfied.

There are no predefined rules stating that a particular situation can be addressed by for loop or by while loop only.

Example:

Suppose we wish to find the sum of first n natural numbers.  Let’s see how we do it using for loop and while loop both.

Output

Sum of first 10 numbers is 55

Output

Sum of first 10 numbers is 55

Here, we can see that the output that we receive is the same but we need to make small number of changes according to the requirements and format of each loop.

Thus, loops provide us the advantage of code re-usability which enables us to execute the same set of statements as many times as we want.

This article is shared and published by Jinal Shah.

About the Author

Jinal Shah

Graduate in Applied Statistics & Analytics (NMIMS) and Aspiring Actuary

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.