In Python, the for loop is also very useful for processing the contents of a list in a sensible manner. consider the following examples
Code:
#!/usr/bin/env python
my_list = ["a", "b", "c", "d", "e"]
# for loop to process the elements of the list
for item in my_list:
print item
# whereas to do the same with a while loop
i = 0
while i < len(my_list):
print my_list[i]
i+=1
I think you'll agree that the for loop is "cleaner", and much better suited to this scenario. The code is much easier to read (by you, and third parties), which is a good practice to keep.
Bookmarks