[ad_1]
Learn how to use Python’s powerful looping construct to make your code more efficient.
The Python for-loop is used to dynamically iterate through a sequence of objects and, for example, perform calculations on them. It automatically deals with different lengths of objects and thus can save work in programming.
In the Python programming language, for-loops are used primarily to work with Python lists to modify objects in the list or use them for other calculations. The advantage of using a loop is that you don’t need to know the length of the list or the individual indexes of the objects.
The for-loop in Python is massively different from what is known in other programming languages. In them, it is used only as an alternative to the while loop, that is, to perform calculations until the condition is satisfied. On the other hand, in Python it is specially designed for working with objects and especially lists.
The structure of a for-loop is always relatively similar and starts with the word “for”. This is followed by a variable name whose value changes with each run. In our case, we call “variable” and pass the object as “object”. At each pass, the variable takes the value of the element currently in the queue. The word “in” separates the name of the variable and the name of the object to be passed:
A line ends with a colon and the next line begins with an indentation. Each line that is indented after the for statement is executed in one pass. Here, calculations can be performed or, as in our example, simply the elements of a list can be output:
If you don’t want to use a for-loop to iterate over a specific object, but just want the command to be executed multiple times, then the range() function is used. It is written in place of the object name and specifies the range of values to be repeated. There are three specifications for this function: range (start, stop, step). So you can specify at which number the function should start, the default is 0. Also, you can specify the value at which the function should stop and how big the step size is, here the default is 1.
If you pass only one value to the range() function, then it is automatically the stop value with an initial value of 0 and a step length of 1.
By passing two values, you set the start and stop values:
As mentioned earlier, all lines of code that are inserted after the for loop are executed on each pass. This allows you to create more complex relationships and include an if-else loop, for example:
So far we’ve learned that Python’s for-loop always has a line break after the colon and continues indenting on the next line. To make the code more compact or to save time, there is also the option to write a Python for-loop in a single line, depending on how complex it is. The above example, which outputs the numbers 2 through 5, can be written very easily in one line:
With more complex for-loops, the one-line representation can also quickly become confusing, as our example with even and odd numbers shows:
It is particularly elegant to create a list or dictionary using a for-loop, which is then usually also defined in a string:
If you solve this with a “regular” Python for-loop, you have to first create an empty list and then populate it one by one. This is much more complicated:
A Python for-loop does not always have to go 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 “Pause” command. In the following example, the loop is terminated as soon as the current number is greater than 4.
The “continue” command is the exact opposite of “break” and causes the loop to run. It makes sense especially if you don’t have a direct command to execute a condition, but just want to start a round in the loop.
In the following example, we want to output only numbers greater than four. To do this, we skip all values that are less than or equal to four using ‘continue’. This will output only numbers above five:
With the help of “enumerate” you can not only iterate over the elements of an object like a list, but also get the index of the corresponding element at once. This can make sense, for example, if you want to directly modify the elements of a list.
Suppose we want to multiply each odd number in the “numbers” list by two. To do this, we iterate over the “numbers” object through the “enumerate” object and replace the number in the list if the element is odd. It is important to note here that now we have to give two names, because each iteration step has two variables, namely the index and the element itself.
It is important to understand here that changes to the object you are iterating over do not affect the Python for-loop. He still looks at the object as he did on the first pass. The following command should otherwise result in an infinite loop, as the “numbers” element becomes twice as long on each pass as it was before. However, the Python for-loop only has nine steps, because at the beginning of the loop the “numbers” object contained only nine elements.
- The Python for-loop is used to dynamically iterate through the elements of an object.
- With the help of “break” you can end the loop prematurely.
- The command “enumerate” not only returns the element in each round, but also the index of the element in the object. This makes it possible, for example, to modify an object from within a loop.
[ad_2]
Source link