← Back to Python Course | Chapter 3: Control Flow | Lesson 5 of 10

Python while Loop

A while loop keeps repeating a job as long as something stays true, like stirring soup until it is hot. When the condition turns false, the loop stops.
Syntax
python
while condition:
    # loop body
    update_variable

Simple While Loop

A while loop re-checks its condition before every pass and keeps running its body as long as that condition stays True; the moment it evaluates to False, the loop exits and execution continues after it.

Example: Simple While Loop

python
count = 0
while count < 3:  # loop continues as long as this stays True
    print(count)
    count += 1  # without this, the condition would never become False

Decrementing Loops

Subtracting from the loop variable each iteration lets a while loop count downward — a common pattern for countdowns, retry limits, or processing a shrinking range of values until it reaches zero.

Example: Decrementing Loops

python
countdown = 3
while countdown > 0:
    print(countdown)
    countdown -= 1  # counts down toward zero each iteration

Using Flags for Loop Control

Using a boolean variable as the loop condition — while running: — lets code anywhere inside the loop body set that flag to False to end the loop, which is more flexible than a fixed numeric condition when the stop point depends on runtime logic.

Example: Using Flags for Loop Control

python
running = True
while running:  # loop continues while the flag is True
    print("Running")
    running = False  # setting the flag False ends the loop next check

While Loop with Lists

A while loop paired with .pop() is a common way to drain a list one item at a time, since the list shrinks with each iteration and the loop naturally ends once it's empty.

Example: While Loop with Lists

python
items = [1, 2, 3]
while items:  # loop continues while the list is non-empty
    print(items.pop())  # removes and prints the last item, shrinking the list

Mathematical Loops

Multiplying or dividing the loop variable each pass (rather than adding or subtracting a fixed amount) produces exponential growth or decay, useful for simulations or algorithms like binary search that repeatedly halve a range.

Example: Mathematical Loops

python
value = 1
while value < 100:
    print(value)
    value *= 2  # doubles each iteration, growing exponentially
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting to update the loop variable, so while i < 5: never ends and runs forever.
  2. Off-by-one conditions such as while i <= 5 when starting at 0, giving one extra iteration.
  3. Modifying a list while looping over it with while and pop/remove, which skips elements or ends early.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.