This post will show you how to use loops in Python.
Loops are a commonly used structure in programming that allows you to repeat a block of code a certain number of times or until you meet a certain condition.
This is useful for many reasons. For example, when creating a game, you may want most of your code to “loop” continuously while you update the enemies’ location and look for player input. When creating a game in Pygame, the “game loop” is one of the most important aspects of this code.
Also read: How to Create a Game in Python: An Introduction to Pygame
More often than not, programmers have to learn to use loops in Python so that they can perform iterative tasks. For example, you might want to check all files in a list one by one, or 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 loops in programming. These are “for” loops and “while” loops. For loops are used to traverse a range – for example, all elements of a list.
You do this by enclosing the “range” in brackets.
for x in range(5): print(x)
This will print the numbers 1-5. If you had a list, you could also use this to go through the list and print out each item:
fruit = ["apple", "orange", "pear", "banana", "pineapple", "grape"] for x in range(5): print(fruit[x])
This shows us the first five items on our list!
We can also use a for loop to display 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 display entries two to five! Remember, however, that the first item in a list is zero.
Finally, we can actually change the iterator – the number of steps executed in each loop.
for x in range(2, 20, 3):
This goes from the second (third) entry to the 20ththe, jump three places each time.
How to use “while” loops in Python
The great thing about Python is that a lot of its instructions sound like simple English, which means you can guess what they’re doing 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 block of code. The block of code is executed repeatedly until the test statement is no longer true:
n = 0 while n < 10: n += 1 print("Done!")
This runs until n = 10. Remember that “<" means less than and n + = 1 means the same thing as n = n + 1.
Oh, and you can always stop the code with “break” or cut your code with “continue” (return to the for or while statement halfway through the code block).
How To Use Loops In Python! It’s pretty simple, but when you combine this with if statements you can achieve all sorts of amazing things! Learn how to do this by reading our extensive and comprehensive guide to the language here: Python Beginner’s Guide – Everything You Need to Know to Get Started!
Also read: Using if statements in Python
Would you like to learn more? Anyone can become a proficient programmer with Python by taking an online Python course. Check out our list of great offers in our guide to the best Python courses online.
For more developer news, features, and tutorials from Android Authority, don’t sign up for the monthly newsletter below!