Loop is a very powerful concept in programming and it allows to execute a task in iterations. Repeating a task or a function for a desired number of times can have many benefits.
In this lesson we will look at Python For Loops.
It can be best explained over a Python example:
>>> for iterator in iterable:
>>> execute task
In a simplified sense, we see three somewhat trivial parts, these are “for”, “in” and “:” in the little code piece above. These are parts of the for loop syntax in Python and they never change. We also see three parts that can change in every different for loop. These are:
- iterator: is a variable we named and it is used to iterate through the collection (list, dict, tuple, range object or something of that sort).
- iterable: is a sequence or collection through which iteration happens in for loops.
- task: this is the part where code executes a task or a function. It’s also what happens during each iteration in the loop. Also please note that this part is always indented by a tab character (sometimes 4 space characters instead of tab on some platforms).