What is a Python for loop used for
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 with Python for loop
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.
Iterate over a string with Python for loop
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 with Python for loop
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!
Iterate over a dictionary with Python for loop
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 dictionary values with Python for loop
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.
Iterate over a dictionary keys and values with a Python for loop
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).
Summary
This is how Python for loop works in practice. If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.
In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.
Programming tutorials
- How to use the Python for loop
- How to use Python Sets
- How to use a Python Dictionary
- How to use Python Classes
- How to use Python Range
- How to use Python if-else statements
- How to use Python RegEx
- How to use Python Lists
- How to use Python Enumerate
- How to use Python Functions
- How to use Python Split
- How to use Python Try-Except
- How to use Python Tuples
- How to use Python Arrays
- How to use Python Sort
- How to use the Python DateTime
- How to download Python?
- How to use the Python FileWrite function
- How to use Python Lambda
- How to use Python ListAppend
- How to use Python ListComprehension
- How to use Python Map
- How to use Python Operators
- How to use Python Pandas
- How to use Python Requests
- How to use Python Strings
- How to use Python Count
- How to use Python Comments
- How to use the Python File Reader method
- How to use the Python IDE-s
- How to use Python logging
- How to use Python Print
- How to use the Python Zip
- How to use Python Append
- How to use Python Global Variables
- How to use the Python join method
- How to use Python list length
- How to use Python JSON files
- How to use Python Modulo
- How to use Python file opening methods
- How to use Python round
- How to use Python sleep
- How to use Python replace
- How to use Python strip
- How to use the Python Time module
- How to use Python unittests
- How to save data to a text file using Context Manager?
- How to use Python external modules
- How to use Python find
- How to use the Python pip package manager
- How to delete files in Python
- Parsing XML files in Python
- How to make a GUI in Python
- How to use Python in Command Prompt
- How to Run a Python Program in VS Code
- How to run a program in Python IDLE
- How to run a program in Jupyter Notebook
- How to read a text file in Python
- How to add numbers in Python
- How to ask for user input in Python
- How to debug in Python
- How to create a thread in Python
- How to import a library in Python
- How to use the PIP package manager
- How to use classes in Python
- How to reverse strings in Python
- How to convert a string to int in Python
- How to print on the same line in Python
- How to remove items from a list
- How to add to a dictionary in Python
- How to raise an exception in Python
- How to throw an exception in Python
- How to stop a program in Python
- How to use Python assert
- How to use the Python compiler
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.
