The progression of the programs written in any programming language is successive by default. Sometimes we might have to modify the flow of the program. The execution of a particular code might should be repeated several numbers of times.

Python loops


For this reason, The programming languages give different types of loops which are capable of repeating some particular code several numbers of times. Think about the following diagram to get the working of a loop statement.


Why we use loops in python programming?

The looping improves on the complex issues into the simple ones. It empowers us to modify the flow of the program so that instead of writing the similar code over and over, we can repeat the similar code for a limited number of times. For example, in the event that we need to print the first 10 natural numbers , rather than using the print statement 10 times, we can print inside a loop which approaches 10 iterations.


Advantages of loops in python

There are the following advantages or benefits of loops in Python Programming.

  1. It gives code re-usability.
  2. Utilizing loops, we don't have to write the similar code again and again.
  3. Using loops, we can traverse over the elements of data structures (array or linked lists).


There are the following types of loop statements in Python Programming.


Loop Statement Description
for loop The for loop is utilized in the case where we have to execute some part of the code until the provided condition is satisfied. The for loop is likewise called as a per-tested loop. It is better to use for loop if the number of iteration is known in advance.
while loop The while loop is to be utilized in the scenario where we do not know the number of iterations in advance. The block of statements is executed in the while loop until the condition specified in the while loop is satisfied. It is also called a pre-tested loop.
do-while loop The do-while loop continues until a given condition satisfies. It is also called post tested loop. It is utilized when it is necessary to execute the loop at least once (mostly menu driven programs).


Loop Control Statements in Python: 

Loop control statements change execution from their ordinary arrangement. At the point when execution leaves a scope, all automatic objects that were made in that scope are destroyed. Python upholds the following control statements. 

Continue Statement: It returns the control to the starting of the loop.

"""Prints all letters 
   except 'i' and 's'"""
for letter in 'sinhacoach':
	if letter == 'e' or letter == 's':
		continue
	print 'Current Letter :', letter
	var = 10


Output: 


Current Letter : n
Current Letter : h
Current Letter : a
Current Letter : c
Current Letter : o
Current Letter : a
Current Letter : c
Current Letter : h


Break Statement: It brings control out of the loop

for letter in 'sinhacoach':

  # break the loop as soon it sees 'i'
  # or 's'
	if letter == 'i' or letter == 's':
		break

print 'Current Letter :', letter


Output: 

Current Letter : i



Pass Statement: We utilize pass statement to write empty loops. Pass is likewise utilized for empty control statements, functions and classes.

# An empty loop
for letter in 'sinhacoach':
	pass
print 'Last Letter :', letter


Output: 

Last Letter : h


Previous                                                                                             Next