PY-101 ยท Loops
while loops
โฑ 8 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก The 30-second answer
A while loop repeats its block as long as its condition is True, checking before every round. Use it when you do not know in advance how many rounds you need. Something inside the loop must eventually make the condition False, or it runs forever.
- Write a while loop
- Update the condition inside the loop
- Avoid infinite loops
Try it
Watch it come alive, run it yourself, then prove it. Real Python, right on this page.
Watch Python run the example. Every variable is a jar; watch the values change and the output appear.
savings = 0months = 0while savings < 10000:savings = savings + 1500months = months + 1print("Months:", months)print("Saved:", savings)
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
Months: 7
Saved: 10500 ๐ฏ A population of 100 bacteria doubles every hour. Use a while loop to find how many hours until it reaches at least 5000. Print the hours.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
while bacteria < 5000: double it and add 1 to hours.
- The problemA savings app asks: how many months until I reach โน10,000 if I save โน1,500 a month? You do not know the number of rounds; you only know when to stop.
- The Python idea
while condition:A while loop repeats its block as long as its condition is True, checking before every round. - In real lifeDownload managers retry a failed download while attempts are under 3, then give up and tell you.
Start with a question
A savings app asks: how many months until I reach โน10,000 if I save โน1,500 a month? You do not know the number of rounds; you only know when to stop.
Games run while the player is alive, servers run while they are switched on, and retries run until something works.
Explain it like I'm 10
It is like eating sweets "while there are still sweets in the bag". You keep going until the bag is empty, however many there were.
The technical explanation
Python checks the condition first. If it is True the block runs, then Python checks again.
Usually a variable changes inside the loop, such as count = count + 1, so the condition becomes False one day.
In this editor a loop that never ends is stopped after a few seconds. On your own computer press Ctrl+C.
Syntax
while condition:
... Real-world scenario
Download managers retry a failed download while attempts are under 3, then give up and tell you.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: break, continue and loop else โQuestions people ask
When should I use while instead of for in Python?
Use for when you loop over a known collection or count. Use while when you stop on a condition and do not know how many rounds it will take.
How do I stop an infinite loop?
Press Ctrl+C in a terminal. In code, make sure the condition can become False, or use break when the job is done.
Interview questions
How this topic comes up in Python interviews. Answer out loud first, then open the model answer.
beginner When should I use while instead of for in Python?
Use for when you loop over a known collection or count. Use while when you stop on a condition and do not know how many rounds it will take.
beginner How do I stop an infinite loop?
Press Ctrl+C in a terminal. In code, make sure the condition can become False, or use break when the job is done.
intermediate When does a while loop stop?
When its condition is False. It runs while the condition is True.
intermediate x = 0; while x < 3: x += 1. What is x after?
3. It stops once x reaches 3.
Quick revision
Remember
- Write a while loop
- Update the condition inside the loop
- Avoid infinite loops
- If a while loop never ends, check that the variable in the condition actually changes inside the loop.
Syntax
while condition:
... Official documentation
- while statement (docs.python.org)