Python’s for loop is used to dynamically iterate over a sequence of objects and perform calculations on them, for example. It automatically deals with different lengths of the objects and thus can save labor in programming.
In it Piton programming language, for loops are primarily used to work with Python Liststo change the objects inside the list or be able to use them for another calculation. The advantage of using the loop is that you don’t need to know the length of the loop. list or the individual indices of the objects.
The for loop in Piton differs greatly from those known in other programming languages. In these, it is only used as an alternative to the while loop, that is, to perform a calculation whenever a condition is met. In Pitonon the other hand, it is specifically thought to work with objects and especially with lists.
The structure of a for loop is always relatively similar and begins with the word “for”. This is followed by the name of the variable, whose value changes with each execution. In our case, we call this “variable” and execute the object “object”. At each step, the variable takes the value of the element that is currently in the queue. The word “in” separates the name of the variable and the name of the object being traversed:
The line ends with a colon, and the next line begins with an indent. Each line that is indented after the for statement is executed in a single pass. Here you can run calculations or, as in our example, you can simply output the list items:
If you don’t want to use the for loop to iterate over a particular object, but just to have a command run multiple times, then the range() function is used. It is written in place of the object name and defines a range of values that is iterated over. There are three specifications for this function: range (start, stop, step). So you can specify what number the function should start at, the default is 0. Also, you can specify what value the function should stop at and how big the step size is, here the default is 1.
If you pass only one value to the range() function, then this is automatically the stop value with start value 0 and step length 1.
When passing two values, set the start value and the end value:
As already described, all lines of code that are indented after the for loop are executed on each pass. This allows you to map more complex relationships and include an if-else loop, for example:
So far we’ve learned that Python’s for loop always has a newline after the colon and continues to indent on the next line. To make the code more compact or to save time, there is also the possibility of writing a Python for loop on a single line, depending on how complex it is. The above example, which generates the numbers between 2 and 5, can be written very easily on a single line:
With more complex for loops, one-line representation can also quickly become confusing, as our example with odd and even numbers shows:
Particularly elegant is the creation of a list or a dictionary using a for loop, which is usually also defined on one line:
If I were to solve this with a “regular” Python for loop, I would first have to create an empty loop list and then fill it little by little. This is much more cumbersome:
A Python for loop should not always execute to the end of the loop. In some cases, it may also make sense to end the sequence early. For this purpose, you can use the “break” command. In the following example, the loop is interrupted as soon as the current number is greater than 4.
The “continue” command is the exact opposite of “break” and causes the loop to continue running. It makes sense especially if you don’t have a direct command to execute a condition, but just want to start one round in the loop.
In the following example, we only want to generate the numbers that are greater than four. To do this, we skip all values that are less than or equal to four using “continue”. This will generate only the numbers five and up:
With the help of “enumerate” you can not only iterate through the elements of an object, such as a list but also get the index of the respective element at the same time. This can make sense, for example, if you want to change the elements of a list directly.
Suppose we want to multiply every odd number in the “numbers” list by two. To do this, we iterate through the “numbers” object using “enumerate” and change the number in the list if the element is odd. It is important to note here that we now need to assign two names since each iteration step has two variables, namely the index and the element itself.
It’s important to understand here that changes to the object you’re iterating over have no effect on Python’s for loop. It still looks at the object as it did on the first pass. Otherwise, the following command would have to result in an infinite loop, since the “numbers” element becomes twice as long at each step as before. However, Python’s for loop has only nine steps, because at the beginning of the loop the “numbers” object had only nine elements.
- Python’s for loop is used to dynamically iterate through the elements of an object.
- With the help of “break” you can end a loop prematurely.
- The “enumerate” command not only returns an element in each round, but also the index of the element in the object. This makes it possible, for example, to change the object from within the loop.