Using loops in Python


Using loops in Python

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 set number of times or until you meet a certain condition.

This is useful for many reasons. For example, when 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, one of the most important aspects of this code is “Game Loop”.

Also read: How To Make A Game In Python: An Introduction To Pygame

In most cases, programmers need to learn how to use loops in Python so that they can perform iterative tasks. For example, you may want to examine all of the 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 can you use loops 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.

You do this by enclosing the “range” in brackets.

For example:

for x in range(5):

This will print the numbers 1-5. If you had a list, you can use this too to go through the list and print out each item:

fruit = ["apple", "orange", "pear", "banana", "pineapple", "grape"]
for x in range(5):

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):

This shows entries two through 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 20ththjump three places each time.

Using while loops in Python

The great thing about Python is that a lot of its statements 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

This runs until n = 10. Remember that “<" less than and n + = 1 means the same thing as n = n + 1.

Oh, and you can always stop the code 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 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!

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 deals 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!

Recent Articles

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here

Stay on op - Ge the daily news in your inbox