Looping statements are used to execute a piece of code repeatedly a specified number of times or until a given condition is met.
The for
loop is the most popular iterative control structure in Python. It makes it possible to iterate over a collection of values, performing a certain task for each value.
for <variable> in <sequence>: <statements>
copy
The <variable>
represents an identifier for the current element in the <sequence>
, within the loop body. The sequence can be any iterable object such as a list, a tuple, range, etc. The <statements>
are the set of instructions that the loop will execute each time it iterates through the sequence. The loop will continue until all elements of the sequence have been exhausted or until the break statement is encountered.
In the above example, the for loop iterates through the list of languages printing each language after each iteration. The loop starts by setting the variable 'language'
to the first item in the list ('Python'), and then prints the value of the language. It then moves on to the next item in the list ('C++'), prints the value, and so on until it has iterated through every item in the list.
For loops with range
Python includes the function range() which returns the integers in the specified range. In cases where we want to iterate over a range of integers, we can use the for loops with range as the sequence.
Iterating over compound sequences
When we want to iterate over an object that is itself made of sequences, we can alias each element in the inner sequence using different variable names.
Note that you can use as much as necessary element aliasing, the only condition is that the number of names need to match the number of elements in the inner sequence.
With dictionaries it is possible to alias the their keys and values separately using the items()
method which returns a tuple of key-value pairs..
The enumerate function
The buitin enumerate() function adds a counter to an iterable , we can use it with for loops in order to keep the current index of the iteration.
The break statement
The break
statement is used when we want to pre-maturely exit a loop. We can use it with for loops in order to exit the loop before the sequence elements are exhausted.
The continue statement
The continue
statement tells a loop to immediately start the next iteration. It skips all the remaining code inside the loop for the current iteration and proceeds directly to the next iteration. We can use this statement to skip some parts of a loop without breaking out of the loop.
The else statement
We can use for loops with the else
statement. The else
block will be executed if the loop terminates successfully without encountering any break
statement.
In the following example the else block is not executed because the loop exits pre-maturely.
Nested for loops
Nesting in loops is when a loop is embedded within another loop. This makes it possible to perform more complex iterations over multiple data sets in a single loop statement.
When a for loop appears inside of another for loop, the execution proceeds through the outer loop, then cycles through the inner loop. This process is repeated until all the outer loop iterations are complete.
Nesting makes it possible to cycle through multiple sequences simultaneously, making it easier to compare different sets of data or iterate through the same set multiple times.
You can have as much nesting as necessary, however it is generally recommended to keep your nesting levels as less as possible. A lot of nesting can easily make your program slow by a huge margin. It is also difficult to read and manage when nesting goes too deep.