Python while Loop
A while loop repeats the code below it as long as the condition is true. It checks the condition again before every pass and stops when the condition becomes false. Use it when you can't know in advance how many times to loop.
while condition:
code to repeat
Repeating while a condition is true
You have to change the variable used in the condition inside the loop, or the loop will never end. Below, n goes down by 1 each time, and when it reaches 0, n > 0 becomes false.
If the condition is false from the start, the loop does not run even once.
When you don't know how many times to loop
while suits jobs where the number of repeats depends on the value, such as "how many times do you halve a number before it reaches 1?" or "what is the sum of all the digits?"
n = 1234total = 0while n > 0: total += n % 10 n //= 10print(total) x, steps = 100, 0while x > 1: x //= 2 steps += 1print(steps)10 6
while True and an end marker
When you need to check the condition in the middle, let the loop run forever with while True: and leave it with break at the point where it should stop. This is the basic pattern for reading input that ends "when a 0 appears".
total = 0while True: x = int(input()) if x == 0: break total += xprint(total)5 8 2 0
15
Python has no do-while. A loop that must run at least once is also written with this while True + break pattern.
Checking the condition as you read
Since Python 3.8 you can use := (an assignment expression) to store a value in a variable and use it in the condition right away. The example above gets two lines shorter.
total = 0while (x := int(input())) != 0: total += xprint(total)5 8 2 0
15
Choosing between for and while
If you go through a list from start to end, or the number of repeats is fixed, for is shorter and safer. You can't forget the line that increases the index.
If you can only describe when to stop as a condition, as in "until some condition holds", use while.
Common mistakes
Not changing the value used in the condition
i = 1
while i <= 3:
print(i)
→ 1 is printed forever (an infinite loop)
Add i += 1 inside the loop. If a program does not stop when you run it, you can interrupt it with Ctrl+C.
Mixing up < and <= and looping one time too few
i = 1
while i < 5:
print(i, end=" ")
i += 1
print()
→ You expected 1 to 5 but got 1 2 3 4
To include 5, write i <= 5. Put the first and last values into the condition yourself and check whether it is true. That catches off-by-one mistakes.
Ending a loop by comparing floats with !=
x = 0.0
n = 0
while x != 1.0 and n < 15:
x += 0.1
n += 1
print(n, x)
→ You expected it to stop after ten passes, but it keeps going until the safety check n < 15 stops it, and prints 15 1.5000000000000002
0.1 can't be stored exactly inside a computer, so adding it ten times does not give exactly 1.0. Compare by size, as in while x < 1.0:, or count the passes with an integer.
Writing = instead of == in the condition
n = 5
while n = 5:
n -= 1
→ SyntaxError
= assigns, and == is the operator that asks whether two values are equal. In a condition use ==, as in while n == 5:.