Loops in Python

Keywords: loops, iterations sets, conditional loops, counting loops, while loop, for loop,

Loops Python Programming

This chapter will look at loops in Python which is a way to repeat an operation an arbitrary number of times. We will see that there are different types of loops that you can use and we will, of course, go through how they are used and when it is best to use the different types of loops.

Loops in Python

When we want to repeat an operation or sequence, we use a loop. With the help of the loop, we can write a program sequence (e.g., a calculation) to be performed while either a particular condition is fulfilled (conditional loop), or a loop that repeats the program sequence a predetermined number of times (counting loop).

The two most common structures for repetition within Python that we will, of course, look at more closely are:

What is a loop?

When you want to repeat an operation or a code sequence several times, you should use a loop. The loop is used to repeat a statement or block of statements as long as a particular condition is true. The term “control flow statement” is often used to refer to loops in Python programming generally. There are usually two different types of loops, namely the conditional loop or the counting loop, that is used. 

How does a loop in Python work?

We can illustrate a loop using the flow chart on the right that shows in a simple way how loops work.

Loops Python Programming
  1. The program sequence begins at Start and then checks if the condition is met
  2. If the condition is met, true, the program performs the operation, if the condition is not met, false, the program proceeds
  3. The loop then repeats the process and returns back to check if the condition is met

Note that we can end up with so-called “infinity loops“, that is, loops that do not proceed in the program (terminated / finished). The condition in the loop is simply never met, and since the condition must be fulfilled for the loop to complete, it runs on. Fortunately, the compiler shuts down the program and gives you an error message, so there’s nothing you need to worry about. It is also good to know that it is fairly simple (and common) error to do.

What is a conditional loop in Python?

We use the condition loop when we want to repeat a code sequence, over and over again, until a specified condition is fulfilled. This means that the code block is executed as long as a condition is met and the loop will after each execution of the code block go back and evaluate if the condition is met. The condition loops that we will take a closer look at in this chapter are the while loop

Example of conditional loops in Python

There are countless uses for conditional loops in Python programming, but if we are going to take some, that will give you an idea of how it can be used. For example, you have programmed a program to run as long as the user does not give the exit command. Thus, there will be a condition that as long as input from the user is not “quit”, the loop will run. 

Furthermore, another example of uses could be that you want to go through a list or collection of data until you find a particular keyword. If you have a large amount of data and are looking for one, or more, elements, you can use a condition loop until you find the right element. Your condition will then be that as long as you have not found what you are looking for, the loop should check the next place in the data list.

What is a counting loop in Python?

The counting loop repeats a code sequence a predetermined number of times. Therefore, we use the counting loop when we want to repeat an operation a specific number of times. You often want to run a code block several times in a row. The counting loop that we are going to look at in this chapter is the for loop.

Example of counting loop in Python

We use counting loops when the number of iterations is fixed. Therefore, we usually use counting loops to go through lists or a predetermined number of iterations. For example, a list of participants and based on information, a command must be executed. It may be a teacher who has a list of his students who continuously updates their grades based on a number of different tests. Instead of having the teacher go through the list manually, the counting loop can do so and then collect and modify the information. 

In general, when it comes to counting loops, it is possible to iterate from the beginning to the end, or vice versa. You can also modify the loop so that you take every other step, or maybe every third step. As you realize, it is possible to adjust the calculation loop to several different purposes.

Infinity loops in Python

So-called infinity loops are loops that never gets ended. The condition of the loop is never met, therefore, the loop continues forever. An infinity loop can be due to several reasons; for example, that you forget to update the variables that are in the loop. However,  your compiler will end the loop. Therefore, it will also cause your program to shut down, and you will receive an error message