Python: for loops

Jason Drummond
5 min readApr 6, 2021

In Python we use a for loop to iterate over a sequence, generally this sequence is a list, dictionary, tuple, set, or string. Traditionally it is used when we have a block of code which we want to repeat a certain number of times. For loops are extremely valuable to us as programmers as they help reduce the number of lines of code we have to write, keep us from writing the same line of code over and over, and also help keep our programs less complex and readable. The general syntax of a for loop is as follows:

for loop syntax

This can be read as follows: for each variable in our sequence execute the expression/s that are in the body of the loop. This might not make the most sense now but once we go over a few examples you’ll get the hang of for loops in no time.

for loop example

Let’s see how for loops works with an example. Let’s say that we have a list of numbers that we would like to know the sum of. Here it is so you can follow along with our line of thinking.

num list

We could accomplish getting this sum by creating a variable called sum then access each element in our list by index and adding it to the sum. As we can see in this example that can get pretty tedious and repetitive, especially if we have a list that has hundreds of numbers in it.

adding without loop

Instead of this repetitive and tedious approach we can use a for loop! Notice how we are essentially writing the same line of code over and over and we have to keep track of the index of our list as we are doing it? This can lead to errors in our program as it is much harder for us to keep track of what we are doing. The loop example of this would look like as follows:

for loop example

Look at how much cleaner that block of code is! The wonderful thing about this for loop is that no matter how long our list is we can still use this small bit of code to find the sum of our list, crazy right? Let’s break down what is going on in the for loop just so we know what is going on. We initialize our variable sum to zero outside of our loop and then specify what sequence we will be iterating over, nuts, and what the variable will be called for each element, num. In the first iteration we add the first element of nums to sum so sum will be equal to 3 after the first iteration. In the second iteration we add the second element of nums to sum, sum will now be equal to 10. We keep doing this for every iteration until we reach the end of our list.

enumerate

There are a few different ways that we can loop over a given sequence. For instance we can do it like we did above where we just have access to the variables of the sequence we are iterating over. We can also us the range function and iterate by index of our sequance. If that doesn’t quite make sense here is an example of what our loop to add our list together would look like if we did it by index

loop by index example

This code is a bit more clunky but if you need to use the index of your sequence for some reason this will work. A better way to do this though is with the enumerate method. Enumerate allows us access each element in our list by a variable and keep track of the index of that element if needed. This method also looks a lot more clean in comparison to the previous example, but ill let the following example speak for itself.

enumerate example

Look how much cleaner and more readable this for loop is! The enumerate method allows us to have an easier time creating the body of our for loop as we know both the index and element of our sequence at every iteration of the loop.

for loops with strings

The for loop doesn’t only work with lists, we can also create a for loop that iterates over every character of a string. For example say we had a string, ‘python’, and we wanted to make the entire string uppercase. We can loop over our string, make each character uppercase, then add it to a new string.

looping over a string

The output of this block of code will be an uppercase version of our original string, or ‘PYTHON’. This is merely an example as we can accomplish the same thing without a for loop but if needed we have illustrated how to loop over a string.

Conclusion

Congratulations! You have successfully learned out to create a for loop in python. As we have shown for loops allow us to execute cleaner looking code, have less repeating blocks of code, and makes sure that we don’t create unnecessary errors in our code trying to keep track of indexes and variables that we have already seen. for loops are for sure going to be a staple of your code so I implore you to start utilizing them in your code and learning the best ways to implement them.

--

--