Repetition Structures in Python
Repetition structures are a type of structure which allows us to repeat a piece of code a specific number of times. Repetition comes up frequently in programming, so we want an elegant way to handle it without copying and pasting code. When we repeat code, the situation we are repeating for often falls into one of two categories.
- We wish to repeat a set number of times. For example, we want to print hello 10 times, or ask the user to input 5 numbers.
- We wish to repeat until a certain condition is met. For example, we want to take user input until they enter "exit", or we want to loop until a list is empty.
As such, there are two types of repetition structures that exist to fit these needs.
For loops are used to satisfy the first category of repetition. If we want to loop for a set number of times, or for each element in a list, a for loop is an appropriate choice. To see how for loops work, we will look at a few example problems.
First, let's look at an example involving lists, as they are often a good structure to practice looping with. Suppose we have a list of food items, and we want to display them to the screen for the user to select one. To do this, it would be easy to use a loop, as we can easily repeat a print statement for each of the items inside of the list. Let's see how this is done in Python.
We start by creating a list and printing the text "Available food items are as follows: ". From here, we create a for loop, using the syntax for variable in list that will loop for each of the items that are in the foods list. The flow of this loop is as follows.
As you can see, this for loop will continue to repeat, if there exists items in the list we are iterating from. Once we have seen every item in the list, we stop, and proceed to the next part of the program. This type of loop is very helpful for situations where you what to iterate a constant set, such as the list in this example, or a string of text for instance. There is a more general version of a for loop that will repeat for a set number of times.
An example of this might be implementing a program to find the power of a number. Since power is repeated multiplication, calculating something like 25 would be the same as multiplying 2 by itself 5 times. To do this, we can use a loop to continue multiplying by 2 until we reach the desired power. The code below shows how this is done in Python.
We take in a base and exponent as user input and convert them into integer values. We then define a variable called answer to keep track of the power value, starting at 1. From here, we define the actual loop, using the syntax for variable in range(variable). You can see that this definition is different from our first for loop. Instead of telling Python to iterate for everything in the list, we are telling Python to iterate a specific number of times. In this example, the user inputs the exponent, and we tell Python to iterate for the same number of times as the exponent inputted. As we iterate, we multiply the answer by the base, and end up with the power that we wanted to calculate.
The for loop using range is very useful when the number of iterations is numeric and known. Aside from this, the for loop using lists is useful for when we want to iterate a set list of data. There is one more type of loop that is often used which allows you to iterate on some sort of condition. This is called a while loop, and it is useful for situations where the data isn't constant, or the condition isn't numeric based.
When we are thinking of while loops, we are thinking of situations where we are looping until some sort of condition is met. Typically, this means things like a list being empty, or a value being found, or something like that. In addition, we can combine conditions, to check if we are for instance at an end of a list or found an element.
To understand this, we can look at an example of a while loop. Suppose that we wanted to search a list for a value inputted by a user and stop when we have either found the value we are looking for or searched the whole list. If we wanted to search the whole list regardless, we could use a for loop, however we want to stop when the value is found, so a while loop is more appropriate. Here is what the Python code looks like.
We start by defining a list and asking the user to input a value to search for. The variable CurrentListPosition keeps track of where we are in the list, and foundValue keeps track of if the value has been found yet or not. The syntax for a while loop is while <condition>. In this case, we have two conditions. We continue to loop while the currentListPosition is less than the length of the list, and the value is not found. On each iteration, we check if the value we are at matches the searched value, if it is, the loop ends as foundValue is set to true. At the end, we either print where the value is found, or print that it wasn't found in the list.
This method of searching a list is very common in applications, as it is more efficient to stop searching when a value is found, compared to always searching the whole list. With while loops, it is possible to setup more complex conditions like this, in order to in turn get more efficient code. The concept of while loops and for loops are overall very important to programming. Almost every program will involve repetition of some type to achieve a goal, so understanding these fundamentals is essential to solving problems with programming.