We have seen the decision making statements such as if,else,elif and ternary operator.Here we will see how to iterate a code or a sequence.The Python ‘for’ loop is use for looping or iterating over a sequence like list,tuple or any collection of objects.
Link :Python while loop
The ‘for’ loop unlike the decision making statement will not have any condition to halt looping over a sequence.The looping continues as long there are elements in the sequence.Also note the Python’s ‘for’ loop iterate over the items in the order they appear in the sequence.
The format of writing Python ‘for’ loop is :
for item in sequence :
The sequence is the name of the sequence which you want to iterate over.
The item stores the items taken from the ‘sequence’
The best way to explain everything is through an example.Consider the code to iterate over the list using the ‘for’ loop.
Link :Python list data type
>>> ls=[23 , 67 , 'Text' , 'happy' , 56.567] >>> for item in ls: print( item ) 23 67 Text happy 56.567
The code is simple, ‘item’ stores the items taken from ‘ls’ and the print() function prints out the item to the output screen.
Lets us consider another example a bit more complex than the one given above.In this example let us try to modify the item of a list sequence.If the list has an item [ 5 ,9 , 10 , 18 , 89] we will change each item to it’s square value.
>>> ls=[ 5 ,9 , 10 , 18 , 89] >>> for item in ls: ls[ ls.index(item) ]=item**2 >>> ls [25, 81, 100, 324, 7921]
The call ls.index(item) is to determine the index of the current ‘item’,if the item is 5 then ls.index(5) returns 0 or if item is 10 ls.index( item ) returns 2 and so on (for more information on list index() method visit List index() method).
The ‘item**2‘ calculate the square of the item which is assigned as the new item value.
More complex example using for loop
In the second example above we tried changing the value of the item using the for loop and we could do so easily.Here let us see another example where we will try to insert an item to the sequence using the for loop.
The example here is simple,we will add the text ‘Newer text’ at the beginning if any text in the item has a length greater than 7.The length of the text is obtained using the function len()
In the first program we will program using the usual approach.
>>> ls=[ 'new' , 'happier' , 'Text' , 'Comprehension'] >>> for text in ls: if len(text)>7: ls.insert( 0 , 'Newer text')
Try running the program you will not see an end even if you press ‘ENTER’ multiple times.This means the code has entered into an infinite loop why?
What actually happens is the code will try to insert the text ‘Newer text’ over and over again thus creating an infinite list.
To prevent this from happening we should use a copy of the list object for the ‘for loop’ to iterate over and do the insertion to the actual list object.One way to get the copy of the list is to use the slice method which is ‘list_object[:]‘.
>>> ls=[ 'new' , 'happier' , 'Text' , 'Comprehension'] >>> for text in ls[:]: if len(text)>7: ls.insert(0 , 'Newer text') >>> ls ['Newer text', 'new', 'happier', 'Text', 'Comprehension']
The ‘for loop’ iterate over ‘ls[:]’ but inserts text to ‘ls’ and so the program works just fine!!
Link :List insert() method