A tutorial on Loops in Python

A loop is a sequence of instructions that will be repeated until a certain condition is reached. I will discuss all the types of loops available in Python.

‘for’ loop in Python

for loop is used when we have some repetitive steps to be done a fixed number of times. Most of the language support for loops. It can be definitive ‘for’ loop or indefinite which is when a particular condition needs to be met.

The example consists of a ‘for’ loop to print numbers(5 numbers) from 0 to 4 by using the range function. When the range is passed with a number as a parameter it will start with 0.

for i in range(5): 
    print(i)
#output: 
0
1
2
3
4

When the range between 1 to 5 is needed then 2 parameters need to be passed i.e range(1, 6).

for i in range (1,6) : 
    print(i)
#output 
1
2
3
4
5

I have a list of items and I need to loop over them one by one. employees are a list of names. The for loop iterates over each of them to print them out.

employees = ["John", "Harry", "Nick"]
for emp in employees: 
    print(emp)
#output
John
Harry
Nick

I will discuss more on tuples in the later session. But for now, let us assume that a tuple is a list of items that cannot be changed at a later time. They use parenthesis instead of square braces.

tupA = ("1",3,"hello ", "Good ", "Morning")
for item in tupA: 
    print(item)

#output 
1
3
hello 
Good 
Morning

for loops can be used on dictionary and many other kinds of data structures to achieve a particular purpose and reducing to write the repetitive statements.

While-do-while loops

while loops

while loop is used when we want to execute a set of statements until a particular condition holds good.

The count is initialized to 0 and we print the count until it reaches 6. I have a simple illustration of the while loop here.

count=0
while count < 6: 
    print(count)
    count = count+1
#output
0
1
2
3
4
5

Python does not have a do-while loop but it can be mimicked in while loop whenever needed.

The break statement

We can use a break statement when we want to exit the loops like for and while.

It immediately comes out of the loop and goes to the next statement after the for/while loop. This is used when we have achieved the purpose of the loop and we have what we need to go ahead with the rest of the program. It is also used when we just want to exit from the loop as there may not be anything meaningful derived by continuing to loop.

Let us go through the code snippet that will tell the user if a particular item is in the list or not. When we find the item in the list we have achieved the purpose so we do not want to waste the processing time by looping all items.

isFlowerPresent = 0 
flowers = ["Rose", "Lily", "daffodils", "jasmine" ]
for flower in flowers :
    if(flower == "daffodils"): 
        isFlowerPresent = 1
        break 
        
if(isFlowerPresent ==1) :         
    print("We have the flower in the list")    
#output 
We have the flower in the list

The continue statement

You should use continue when you want to continue running the loop but want to skip the next statements after continue.

Example: Tony does not like Apples. So we do not want to present him with a list of choices with apples. We can use continue to demonstrate this.

fruits = ["mango", "papaya","apple", "oranges", "banana"]
for fruit in fruits :
    if(fruit == "apple"): 
        continue
    print(fruit)

#output
mango
papaya
oranges
banana

I would like to conclude at this point now. We have the knowledge of using these loops in our real coding exercise. I would suggest you to try various scenarios and ideas to learn this concept. Feel free to post the examples in the comments.

Go back to Python tutorial Overview

Prev: Built-in Datatypes in Python

Next Tutorial: List in Python