Methods to use loops in Python


This post shows you how to use loops in Python.

Loops are a structure often used in programming that allow you to repeat a block of code a specified number of times or until you meet a certain condition.

This is useful for many reasons. For example, if you're creating a game, you may want most of your code to "repeat" continuously as you update the enemies' location and look for player input. When creating a game in Pygame, Game Loop is one of the most important aspects of this code.

Read also: How to Create a Game in Python: An Introduction to Pygame

More often than not, programmers need to learn how to use loops in Python to do iterative tasks can perform. For example, you may 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 are loops used in Python!

Using "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 in a list.

To do this, put the "area" in brackets.


for x in the range (5):
print (x)

This prints the numbers 1-5. If you had a list, you could also use that to go through the list and print out each item:

obst = [“apple”, “orange”, “pear”, “banana”, “pineapple”, “grape”]

for x in the range (5):
print (obst [x])

This shows us the first five items on our list!

We can also use a for loop to display a section of items using two numbers in our range: a starting number and ending number.

fruit = [“apple”, “orange”, “pear”, “banana”, “pineapple”, “grape”]

for x in the range (2, 5):
print (Frucht [x])

Entries two to five are displayed here! Remember, however, that the first item in a list is zero.

Finally, we can actually change the iterator – the number of steps performed in each loop.

for x in the range (2, 20, 3):

This goes from the second (third) entry to the 20th and jumps three places each time.

Using "while" loops in Python

The great thing about Python is that a lot of its statements sound like plain English, which means you can guess what they're doing before you do learn at all!

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 code block 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, "<" less than and n + = 1 means the same thing as n = n + 1.

Oh you can also stop the code at any time by using "break" or truncating your code (return to the for or while statement in the middle of the code block) by using "continue".

How To & # 39; Use Loops in Python! It's pretty simple, but when you combine this with if-statements you can accomplish all kinds of amazing things! Read our extensive and comprehensive guide to the language here: Python Beginner's Guide – Everything You Need to Know to Get Started!

Read also: Use if statements in Python

Want to know more? Anyone can become a proficient programmer with Python by taking an online Python course. Check out our list of great deals in our guide to the best Python courses online.


Please enter your comment!
Please enter your name here