How to Repeat Code While a Condition Is True in Python with while
Sometimes you don't know in advance how many times a piece of code has to run: keep saving until the jar is full, keep reading until the text ends. A while loop repeats a block of code for as long as a condition stays true, and stops when it turns false. This guide does it on Python 3.15.2.
Syntax
while <condition>:
<code to repeat>
| Part | What it means |
|---|---|
while | Starts the loop |
<condition> | A check that is true or false, such as saved < 20. Python makes this check before every turn |
: | Ends the while line |
<code to repeat> | The lines that run on every turn. They're indented by four spaces, and something in them has to change what the condition checks |
Python's language reference says: "The while statement is used for repeated execution as long as an expression is true". The tutorial puts it this way about its own example: "The while loop executes as long as the condition (here: a < 10) remains true."
The Example
A savings jar starts empty. Every turn adds 8 to it, and the loop goes on while the jar holds less than 20. The script prints the amount after each turn, then Done.
Prerequisites
- uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.
Step 1: Write the Script
saved = 0
while saved < 20:
saved = saved + 8
print(saved)
print("Done")

The two indented lines belong to the loop. print("Done") isn't indented, so it runs once, after the loop has finished.
Step 2: Run It
uv run python jar.py
8
16
24
Done

The loop ran three times. Python checked the condition before each turn:
| Before turn | saved | Is saved < 20 true? | What happened |
|---|---|---|---|
| 1 | 0 | Yes | Added 8 and printed 8 |
| 2 | 8 | Yes | Added 8 and printed 16 |
| 3 | 16 | Yes | Added 8 and printed 24 |
| 4 | 24 | No | The loop ended, and Done was printed |
The last amount is 24, not 20. Python checks the condition only before a turn, never in the middle of one, so the third turn started with 16 and ran to its end. The command works unchanged in PowerShell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Nothing in the loop changes what the condition checks | With only print(saved) in the loop, the script printed 0 again and again and never stopped. I ended it with Ctrl+C, which printed a traceback ending in KeyboardInterrupt | Change the value on every turn, as saved = saved + 8 does |
| Expecting the loop to run at least once | With saved = 30 at the start, the loop didn't run at all and saved stayed 30 | That's by design: the condition is checked before the first turn too |
| Expecting the loop to stop exactly at the limit | The jar ended at 24, past the limit of 20 | Check the value after the loop, or choose the step so that it lands on the limit |
FAQ
When should I use while instead of for?
Use for when you have a list of items or know the number of turns. Use while when the loop should go on until something happens and you can't tell beforehand how many turns that takes. In the jar example, a different amount per turn would change the number of turns, and the while loop would still stop at the right time.
How do I stop a loop that never ends?
Press Ctrl+C in the terminal. Python stops the script and prints a traceback whose last line is KeyboardInterrupt. Then fix the loop so that its condition can turn false.
Can I leave a while loop early?
Yes, with break. A copy of the jar loop with if saved > 10: and break after the adding line stopped at 16 instead of 24, because the second turn reached break.
Can the condition be a plain value instead of a comparison?
Yes. The tutorial says: "any non-zero integer value is true; zero is false." A loop written as while count: that started with count = 3 and took 1 away on every turn ran three times and stopped when count reached 0.
Conclusion
while saved < 20: repeated its two lines for as long as the jar held less than 20: three turns, printing 8, 16 and 24. The check happens before every turn, which is why the loop can end past the limit, and why it doesn't run at all when the condition is false from the start.
Additional Resources
- The while statement — the language reference.
- First Steps Towards Programming — the tutorial's first
whileloop. - break and continue Statements — leaving a loop early.
