Python while

Jakob Jenkov
Last update: 2024-04-27

The Python while loop is used to repeat a set of Python instructions as long as a given condition is true. When the condition becomes false, the Python interpreter will skip over the body of the while loop, to the first Python instruction after the while loop.

Here is an example Python while loop:

myvar1 = 0
myvar2 = 10

while myvar1 < myvar2:
    print(myvar1)
    myvar1 = myvar1 + 1

This Python while loop will execute 10 times, resulting in myvar1 having the values 0 through 9.

continue

The Python continue keyword can be used inside a Python while loop to make the interpreter skip to the next iteration of the while loop. In other words, the interpreter jumps from the continue instruction to the top of the while loop again, evaluates the condition guarding the while loop, and if true - executes the while loop's next iteration.

Here is an example of the Python continue instruction being used inside a Python while loop:

myvar1 = 0
myvar2 = 10

while myvar1 < myvar2:
    myvar1 = myvar1 + 1
    if myvar > 5:
        continue
    print(myvar1)

This Python while loop will execute 10 times. The first 5 iterations it will print the value of myvar1, after 1 has been added to it. This means the values 1 through 5 will get printed out. Once myvar1 becomes larger than 5, the continue instruction will get executed, and the last print instruction will get jumped over.

break

The Python break instruction can be used inside a Python while loop to break out of the while loop entirely. When the break instruction is executed the Python interpreter will continue at the first instruction following the while loop. Here is an example Python while loop with a break instruction inside:

myvar1 = 0
myvar2 = 10

while myvar1 < myvar2:
    if myvar1 > 5:
        break
    print(myvar1)

In the above Python while loop example - if the myvar1 value becomes larger than 5 the break instruction will be executed. This will cause the Python interpreter to exit the while loop and jump to the first instruction after the while loop. The above example does not have any instruction after the while loop, but if it did, that instruction would get executed.

else

The Python while loop supports having an else clause attached to it. This else clause is executed when the while condition becomes false. If the while loop is exited via a break or return instruction, the else clause is not executed. Here is an example Python while loop with an else clause attached:

myvar1 = 0
myvar2 = 10

while myvar1 < myvar2:
    print(myvar1)
    myvar1 = myvar1 + 1
else:
    print("end of while loop")

Since there is no break or return instruction in the above Python while loop example, the else clause attached to the while loop will get executed.

while Loops in Other Languages

For the curious reader, here are links to tutorials about while loop instructions in all the programming languages covered here at Jenkov.com:

Jakob Jenkov

Featured Videos

Java ConcurrentMap + ConcurrentHashMap

Java Generics

Java ForkJoinPool

P2P Networks Introduction

















Close TOC
All Tutorial Trails
All Trails
Table of contents (TOC) for this tutorial trail
Trail TOC
Table of contents (TOC) for this tutorial
Page TOC
Previous tutorial in this tutorial trail
Previous
Next tutorial in this tutorial trail
Next