We’re going to be looking at “Python for loops”. A for loop is a way of looping through or iterating over a string, a list, or a dictionary and executing some code on each individual part. 

 

Iterate over a list – Python for loop list

Let’s take a look at an example where we have created a list and it’s called dogs. With a simple for loop, we’ll loop through each one of the values in the list and print out each individual one to the screen.

 

 

In Python, a for loop always starts with the word for. Then we need to create a new variable that we’re going to save that data in every time we loop through each individual thing in the list. So, in our case, because our list is dogs, we’re going to call the variable dog. The next word we need is in, and then we need to type the name of our list, dogs. To end the first line in the for loop, we use a colon and start a new line. 

Then we’re going to write what we’re going to do with each of the individual items in this list. In this case, we’re just going to print them out to the screen. We type print(dog). For each iteration through this loop, we’re going to store the first value from the “dogs” list in the dog variable and then print it out, then the second one and print it again. So if we run this code, you’ll see that we have printed out each and every individual dog breed from our list. 

 

Python for loop

 

Iterate over a string – for loop string

We can also loop through individual strings. Let’s say we wanted to loop through a word or a string. 

 

 

We write for d in, then we paste our word ‘Labrador’, use the colon, and in a new line we write print (d). When we run this, it is going to look through every individual character in the string, assign it to the variable d and print that out. This can be quite useful. 

Iterate over a list of numbers – for loop list of numbers

Another example of where you can use a for loop is for numbers. 

 

 

Let’s create a numbers list. We enter number = []. Let’s add some random numbers between the brackets and we’re going to loop through each one of these numbers and execute a bit of code on each one to see the result.

Enter for x in number:, new line, y = x + 5. At the end of the code let’s add print (y). For each and every integer or number, it’s going to assign it to x. Then y is going to become that number plus 5. 

When we run this, we will see that our first one is 6. That’s because 1 plus 5 is equal to 6. The last one in the original list was 5 where we can see 5 was added to it, and now the answer is 10!

 

Python for loop

Iterate over a dictionary – for loop dictionary

Iterate over a dictionary keys

Let’s try iterating through a dictionary. Continuing along the dog’s theme, we can assign names to each of these breeds, then print out the keys of this dictionary and loop through those individually.

 

 

Let’s write for k in dogs.keys()k is the variable, dogs is our dictionary and we add .keys(), because we want to have only the keys of this dictionary in k.

This will loop through each of the keys in the dictionary. When we print k out, we get the dog breeds, and not the names, as these are the keys of the dictionary.

Iterate over a dictionary values

If we want to just print the values, we could write dogs.values() instead of dogs.keys().

 

 

When we run this and we get the values of our dictionary, the dog names.

 

Python for loop

Iterate over a dictionary keys and values

If we wanted to print out both the keys and values, we could loop through the keys and print out both by printing k as the key and dogs[k] as the value.

 

 

We want to access the value for a specific key so we can use dogs[k], which is how you

access the key in a dictionary.

When we run this, we should get the keys of our dictionary (the dog breeds) followed by the values (the dog names).

In our Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.