Python : Loops and iteration

In python loops or iteration can be achieved using following methods

  • while statement
  • for statement
While statement
  • Loop will run till condition is satisfied, once condition is satisfied, it will not execute the loop.
  • Values are checked before execution of block of code
count = 3

print('Starting while loop') 
while count < 5: 
      print('count is : ', count)
      count =count + 1

output

$ python3.6 loop02-while.py 
Starting while loop 
count is : 3 
count is : 4

For statement

following are different combination of for statement

cities = ['Pune','Mumbai','Hyderabad','Delhi','Bangalore']

for city in cities: 
    print('Current city is : ', city)

fname = 'Jasaon'

for char in fname: 
    print('char is : ', char)

for number in range(0, 10): 
    print('Current number is : ', number)

for x in range(2, 20, 3):
    print('Output is : ',x)

And output is

$ python3.6 loop02-for.py 
Current city is : Pune 
Current city is : Mumbai 
Current city is : Hyderabad 
Current city is : Delhi 
Current city is : Bangalore 
char is : J 
char is : a 
char is : s 
char is : a 
char is : o 
char is : n 
Current number is : 0 
Current number is : 1 
Current number is : 2 
Current number is : 3 
Current number is : 4 
Current number is : 5 
Current number is : 6 
Current number is : 7 
Current number is : 8 
Current number is : 9 
output is : 2
output is : 5
output is : 8
output is : 11
output is : 14
output is : 17
Control statements

pass

The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute

continue

Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.

break

Terminates the loop statement and transfers execution to the statement immediately following the loop.

Sample program
rollNumbers = [0,1,2,3,4]


print('For loop with pass statemend started')  
for x in rollNumbers:  
    print('Current Number is :', x)
    if x == 2:
        pass
    print('This is inside loop for pass', x)
print('For loop with pass statemend ended')        

print('For loop with continue statemend started')  
for x in rollNumbers:  
    print('Current Number is :', x)
    if x == 2:
        continue
    print('This is inside loop for continue', x)
print('For loop with continue statemend ended')        

print('For loop with break statemend started')  
for x in rollNumbers:  
    print('Current Number is :', x)
    if x == 2:
        break
    print('This is inside loop for break', x)
print('For loop with break statemend ended')  

output is as below

$ python3.6 loop02-pass-continue-break.py 
For loop with pass statemend started  
Current Number is : 0  
This is inside loop for pass 0  
Current Number is : 1  
This is inside loop for pass 1  
Current Number is : 2  
This is inside loop for pass 2  
Current Number is : 3  
This is inside loop for pass 3  
Current Number is : 4  
This is inside loop for pass 4  
For loop with pass statemend ended  
For loop with continue statemend started  
Current Number is : 0  
This is inside loop for continue 0  
Current Number is : 1  
This is inside loop for continue 1  
Current Number is : 2  
Current Number is : 3  
This is inside loop for continue 3  
Current Number is : 4  
This is inside loop for continue 4  
For loop with continue statemend ended  
For loop with break statemend started  
Current Number is : 0  
This is inside loop for break 0  
Current Number is : 1  
This is inside loop for break 1  
Current Number is : 2  
For loop with break statemend ended  

Please note:
pass : nothing changes, pass statement is nothing but filler used for syntactical requirement

continue: This skips further processing in loop for current iteration only and it jumps to next iteration. Please note, second print statement is not printed for number 2

break: This terminates current iteration as well as whole loop and goes to next statement in python program

Leave a Reply

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