Loops In Python

Introduction

In this article, you will learn that programming language statement is executing sequentially, which means first statement is executing first then second so on.

If we want to execute one statement more than one time, loops provide a facility to execute the statement according to our condition.

In other words, an iteration of single statement or a block of statement can be used for the looping statement in Python.

Flow chart



Python supports following loop statement
  • while loop
  • for loop
  • nested loop
While loop

While loop is used for an iteration of the statement according to the condition. While loop checks condition first, if condition is “true”, it executes the statement.

Flow chart



Syntax

while condition:
          statement(s)

key note statement(s) can be individual or group of statement.

Example
  1. n = int(input("enter a number"))  
  2. i = 1  
  3. while i <= 10:  
  4.     print(n, "X", i, "=", i * n)  
  5.     i = i + 1  
Output


Explanation

In the example, shown above, a user enters a number and prints a table of the number. While loops execute 10 times and prints the table of the number.

for loop

“for loop” has an ability to iterate the item of sequence. For example, list or string etc. “for loop” iterates an item in a sequence.


Syntax

for iteration_variable in sequence:
         statements(s)

Example
  1. tech=[".net","asp.net","c#.net","mvc"] #list of technology  
  2. for item in tech: #for loop "item is iteration variable" and "tech is list  
  3.      print("technology name: ",item)  
  4. print("end of list")  
  5.    
Output


Explanation

For loop iterates every item from the list.

Example

Print the table for a number, using for loop.
  1. n = int(input("enter a number"))  
  2. for i in range(111):  
  3.     print(n, "x", i, "=", n * i)  
Output



Explanation

In this program, a user enters a number to print the table of an input number and ‘for loop’ iterates ten times according to range() function and finally prints table of the number.

Nested loop

In Python programming language, nested loop is one loop inside another loop.

We put any type of loop inside any other type of loop in Python for example we put “for loop” inside “while loop” or vice versa.

Generally, it is used for pattern print

Syntax 

“while loop” inside “while loop”

while condition:
        while condition:
               statement(s)

Example

“while loop” inside “while loop”
  1. str = ''  
  2. i = 1  
  3. while i <= 5:  
  4.     j = 1  
  5.     while j <= i:  
  6.       str = str + "*"  
  7.       j = j + 1  
  8.     i = i + 1  
  9.     str = str + "\n"  
  10. print(str)  
Output


Explanation

In this program outer ‘while loop’ iterates five times and inner ‘while loop’ iterates according to the condition outer ‘while loop’ and ‘*’ is add to ‘str’ in inner ‘while loop’ according to iteration and ‘\n’ is also added in every iteration of outer ‘while loop’.

Syntax

“for loop” inside “while loop”

While condition:
           for iteration_variable in sequence
                 statement(s)

Example

“for loop” inside “while loop”
  1. str = ''  
  2. i = 1  
  3. while i <= 5:  
  4.     for j in range(0, i):  
  5.       str = str + "*"  
  6.     i = i + 1  
  7.     str = str + "\n"  
  8. print(str)  
Output



Explanation

In this program, ‘while loop’ iterates five times and ‘for loop’ iterates according to the condition of ‘while loop’ and ‘*’ is added to ‘str’ in ‘for loop’, according to iteration and ‘\n’ is also added in every iteration of ‘while loop’.
 
Syntax

“for loop” inside “for loop”

for iteration_variable in sequence:
       for iteration_variable in sequence:
             statement(s)

Example

“for loop” inside “for loop”
  1. str = ""  
  2. for i in range(16):  
  3.     for j in range(0, i):  
  4.       str = str + "*"  
  5.     str = str + "\n"  
  6. print(str)  
Output


Explanation

In this program ‘for loop’ iterates five times and inner ‘for loop’ iterates according to iteration variable of outer ‘for loop’ and ‘*’ is added to ‘str’ in inner ‘for loop’, according to iteration and ‘\n’ is also added in every iteration of outer ‘for loop’.

Syntax 

“while loop” inside “for loop”

for iteration_variable in sequence:
         while condition:
                  statement(s)

Example

“while loop” inside “for loop”
  1. str = ''  
  2. for i in range(16):  
  3.     j = 0  
  4.     while (j < i):  
  5.       str = str + "*"  
  6.       j = j + 1  
  7.     str = str + "\n"  
  8. print(str)  
Output


Explanation

In this program, ‘for loop’ iterates five times and ‘while loop’ iterates according to iteration variable of ‘for loop’ and ‘*’ is added to ‘str’ in ‘while loop’ , according to iteration and ‘\n’ is also added in every iteration of ‘for loop’.

Loop control statement

In Python, control statement is used to change the execution of the loop for its normal sequence.

Python supports the following control statements.
  • break statement
  • continue statement
  • pass statement
break statement

Break statement is used to terminate the loop on the certain condition and next statement is executed as well, which means it only terminates the block of loop and does not effect on any other statement outside the block of the loop.

Break can be used with “for loop” as well as “while loop”.


Syntax

break #We only write break for the termination of the loop and the block of the loop.

Example
  1. list = ['.net''C#''asp.net''mvc']  
  2. name = input("enter a technology for search")  
  3. for item in list:  
  4.     if item == name:  
  5.       print("name is found")  
  6.       break  
  7.     else :  
  8.       print(name, "is not found.. wait check in next item")  
Output



Explanation

In the program, mention the user, enter a technology and search in the list, “for loop” iterates all the item of the list and technology is found then “for loop” is terminate.

Continue statement

Continue statement is used to skip statement inside the loop block and continue statement returns control of the loop and moves back to beginning.

Control statement is used with “for loop” and also while loop.



Syntax

Continue # only write continue.

Example
  1. n = int(input("enter a range"))  
  2. i = 0  
  3. print("\neven number are in range 1 to", n)  
  4. while i <= n:  
  5.     i = i + 1  
  6.     if i % 2 != 0:  
  7.       continue  
  8.     print(i)  
Output



Explanation

In the program, shown above, the user enters a range and while loop iterates the loop according to the range and “if condition” checks the number, which is odd, if this condition is skipped, using continue statement and prints only even number.

Pass statement

Pass statement is null operation, pass statement does nothing on execution but it is used as a placeholder in our program, when we need a statement syntactically.

Syntax

pass #only write pass

Example
  1. for i in 'c#corner':  
  2.   if i=='#':  
  3.     pass  
  4.   print("charater=",i)  
Output



Explanation

In the program, mentioned above, we put pass statement in “if condition” but if condition does not affect the sequence and the sequence is printed exactly same.

Summary

In this article, you learnt the concept of looping, types of loops like while loop and for loop etc.