Python “while” Loops [with Examples]

python while loops

This tutorial explains how to create loops in Python by using the “while” statement. Included below are the syntax and examples.

In programming (and thus, in Python), a loop is a statement which will execute a block of code repeatedly until a condition is met, or the loop is broken out of manually.

This article will look at Python Loops and conventions to use when writing them.

Looping with the while statement, Syntax and Example

Here is a basic loop which will increment (add 1) the variable ‘i’ until it reaches the number ‘7’:

i = 1
while i <= 7:
    print(i)
    i += 1

Note that:

  • This example will print numbers 1-7 sequentially
  • i is the name of the variable we are incrementing
    • i is often used in programming as the name of a value being iterated if no better name with a clearer meaning is available
    • If i is already in use, j may be used, and so on
  • Remember to increment i, otherwise, the loop will run infinitely

Using break to Exit a Loop

The break statement will exit the loop at a given point, even if the loop condition is still ‘True’:

i = 1
while i <= 7:
    print(i)
    if i == 5:
        break
    i += 1
  • The above code will exit (break) the loop when i reaches 5, even though it is still less than 7.
  • This means it will only print the numbers 1-5 sequentially

Using continue to Skip an Iteration

The continue statement will stop executing the code in the current iteration of the loop (skipping it entirely), and continue on to the next iteration:

i = 0
while i <= 7:
    i += 1
    if i == 5:
        continue
    print(i)
  • The above code will continue executing when i reaches 5skipping over the remaining code for that iteration of the loop and continuing at the next iteration (when i is 6).
  • This code example will output the numbers 1,2,3,4,6,7,8
    • 8 is now printed because the print statement was moved after we increment i
    • This was necessary, as if i was incremented after the continue statement, we would never increment again after i reached 5, causing an infinite loop and freezing the program!

Using else to Indicate when the Loop Condition Changes

The else statement will run the associated code when the loop condition is no longer True – which more often than not will be when the loop has finished without the use of the break statement:

i = 1
while i <= 7:
    print(i)
    i += 1
else:
    print("i reached the count of 7")

Conclusion

Managing Lists (Arrays) and Loops usually forms the building blocks of modern computer software. If you’re writing business software, you might be looping over lists of transactions, if you’re writing games, you might be looping over lists of players and enemies to see where they are in the game world.

For more Python tutorials, click here!

SHARE:
nv-author-image

Brad Morton

I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Reply

Your email address will not be published. Required fields are marked *