In this post, you will learn how to use loops in Python.
Loops are a commonly used structure in programming that allows you to repeat a block of code a set number of times, or until you meet a particular condition.
This is useful for many reasons. For example, if you are building a game, then you might want the bulk of your code to continuously “loop” while you update the position of enemies and check for player inputs. If you build a game in Pygame, then “game loop” will be one of the most important aspects of that code.
More often, programmers need to learn how to use loops in Python so that they can perform iterative tasks. For instance, they might want to individually check all the files in a list, or they might want to look for something in a database.
Now you know what a loop is, the next question is how to use loops in Python!
How to use “for” loops in Python
There are two main types of loop across programming. These are “for” loops and “while” loops. For loops are used to run through a range – for instance all the items on a list.
You do this by setting the “range” in brackets.
for x in range(5): print(x)
This will print out the numbers 1-5. If you had a list, then you could likewise use this to run through the list and print each item:
fruit = ["apple", "orange", "pear", "banana", "pineapple", "grape"] for x in range(5): print(fruit[x])
This will show us the first five items on our list!
We can also use a for loop to show a section of items by using two numbers in our range: a starting number and an ending number.
fruit = ["apple", "orange", "pear", "banana", "pineapple", "grape"] for x in range(2, 5): print(fruit[x])
This will show entries two through five! But remember that the first item in a list has the index “zero.”
Finally, we can actually change the iterator – the number of steps taken on each loop.
for x in range(2, 20, 3):
This will go from the second (third) entry up to the 20th, jumping three places each time.
How to use “while” loops in Python
The great thing about Python is that a lot of its statements sound like plain English, meaning you can guess what they do before you even learn!
A “while loop” runs “while” something is true.
Here, you write “while” followed by a test statement, a colon, and a code block. The code block runs repeatedly until the test statement is no longer true:
n = 0 while n < 10: n += 1 print("Done!")
This will run until n = 10. Remember that “<” means smaller than, and n += 1 means the same thing as n = n + 1.
Oh, and you can also stop the code at any time by using “break” or truncate your code (return to the for or while statement halfway through the code block) using “continue.”
So that is how to use loops in Python! It’s pretty simple, but if you combine this with if statements, you can accomplish all kinds of amazing things! Find out how to do this by reading our huge and comprehensive guide to the language here: Python beginners’ guide – Everything you need to know to get started!
Also read: How to use if statements in Python
Want to learn more? Anyone can become a competent programmer using Python by taking an online Python course. Check out our list of amazing deals at our guide to the best online Python courses.