Loading...

Python For Loops


in this tutorial we are going to learn what are for loops in python, break and continue in python for loop, else in for loop in python


What is a for loop in python ?


in python the for loop is used for iterating and traversing a sequential object which is in the form of strings, lists, tuple, dictionary etc.
it continues the iteration until it reaches the last item index position.

syntax of the for loop is :
        		     					    		
for x in obj:
	statements block

        		     					    		
        		     					    	
in for loop there is no index variable because in python it does it implicitly
in the above syntax x is a variable which gets a new value over each iteration and pass it to the suite (statements block) of the for loop.
for example:
        		     					    		
listobj = ['jenny', 'peter', 'harry', 'maria']

for x in listobj:
	print(x)

output:

jenny
peter
harry
maria

        		     					    		
        		     					    	
let's explain the above example in simple steps
- listobj is a list in python program.
- x in for loop is a variable.
- python implicitly checks the length of the listobj which is 4. so for loop iterates 4 times in the program
- in python the list is internally index position starts from 0.
- when iteration starts at 0 position in list
    variable x gets item at position listobj[0] which is x = jenny ( after iteration 1 )
- when iteration starts at 1 position in list
    variable x gets item at position listobj[1] which is x = peter ( after iteration 2 )
- when iteration starts at 2 position in list
    variable x gets item at position listobj[2] which is x = harry ( after iteration 3 )
- when iteration starts at 3 position in list
    variable x gets item at position listobj[3] which is x = maria ( after iteration 4 )


break and continue in python for loop


break
in python the break keyword is used to stop the execution of the for loop statements block and pass the control to the next statements in the program.
for example:
        		     					    		
listobj = ['jenny', 'peter', 'harry', 'maria']

for x in listobj:
	if x == 'jenny':
		continue
	print(x)

print("msg : for loop execution finish")

output:

peter
harry
maria
msg : for loop execution finish

        		     					    		
        		     					    	
in the above example it is clear that when variable x value is equals to listobj[0].
then break statement stops the execution of the iterations in the for loop and moves to the next print statement

continue
in python the continue keyword is used to stop the execution of the for loop current iteration and move to the next iteration.
for example:
        		     					    		
listobj = ['jenny', 'peter', 'harry', 'maria']

for x in listobj:
	print(x)

output:

jenny
peter
harry
maria

        		     					    		
        		     					    	
in the above example it is clear that when variable x value is equals to listobj[0].
then continue statement stops the execution of the iteration in the for loop and moves to the next next iteration



else in for loop in python


else in for loop is used when the execution of the for loop in python is complete.
for example:
        		     					    		
listobj = ['angelina', 'johny', 'robert', 'smith']

for x in listobj:
	print(x)
else:	
	print("for loop execution is complete now")

output:

angelina
johny
robert
smith
for loop execution is complete now

        		     					    		
        		     					    	
in the above example when the for loop finishes its execution the print statement is executes.