Lesson 15 of 60
โฎPrev Nextโฏ

PY-101 ยท Loops

while loops

โฑ 8 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

15of 60

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก 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.

    while loops, running
    1. savings = 0
    2. months = 0
    3. while savings < 10000:
    4. savings = savings + 1500
    5. months = months + 1
    6. print("Months:", months)
    7. print("Saved:", savings)
    Press โ–ถ to watch Python run this, one line at a time.
    1. 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.
    2. The Python ideawhile condition:A while loop repeats its block as long as its condition is True, checking before every round.
    3. 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:
        ...

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    Real-world scenario

    Download managers retry a failed download while attempts are under 3, then give up and tell you.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. When does a while loop stop?
    2. 2. x = 0; while x < 3: x += 1. What is x after?
    3. 3. What causes an infinite loop?
    4. 4. When is the condition checked?
    5. 5. Best loop when you do not know how many rounds?

    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