Skip to main content
  • Python is available now. All other compilers are coming soon.

    Python
  • Popular Tutorials

    • Getting Started With Python
    • Python if Statement
    • while Loop in Python
    • Python Lists
    • Dictionaries in Python
    All Python Tutorials

    Reference Materials

    Built-in FunctionsList MethodsDictionary MethodsString Methods
    View all

Online Compilers

  • Python Compiler
  • Python Playground
  • Python Projects
  • Exercises

Tutorials

  • Learn Python
  • Python for Beginners
  • Variables and Types
  • Python Strings
  • Python Lists
  • Python If Else
  • Python For Loops
  • Python Functions
  • Python Classes

Examples

  • Python Examples
  • Hello World
  • Add Two Numbers
  • Even or Odd
  • Factorial
  • Fibonacci
  • Prime Check
  • Palindrome Check

Company

  • About
  • Contact
  • Privacy Policy
  • Terms of Use
  • Disclaimer

© 2026 ILoveCompiler. All rights reserved.

  1. Home
  2. Python Tutorial
  3. Python while Loops

Control flow · Tutorial 10

Python while Loops

Repeat code until a condition changes, avoid infinite loops, and use counters and sentinels safely.

Open the compilerPractice exercises
beginner8 min readUpdated November 19, 20263 learning goals
On this page
  1. Accumulating a result
  2. Reading until a sentinel
  3. while versus for
  4. Validating input
  5. Practice

What you will learn

  • Write a while loop with a changing condition
  • Use a counter and an accumulator
  • Recognise and prevent infinite loops
Before this lesson: Python if, elif, and else Statements

A while loop repeats a block as long as its condition remains true.

Python
count = 1

while count <= 5:
    print(count)
    count += 1
Expected output

1 2 3 4 5

Three things keep the loop finite:

  1. The condition is initially true.
  2. The body changes a value used by the condition (count += 1).
  3. The condition eventually becomes false.

If a loop never changes its condition, it runs forever (or until ILoveCompiler's time limit stops it).

Accumulating a result#

Python
total = 0
number = 1

while number <= 100:
    total += number
    number += 1

print(total)

total is an accumulator. Initialise it before the loop and update it inside.

Reading until a sentinel#

A sentinel is a value that signals the end of input:

Python
total = 0

while True:
    text = input()
    if text == "done":
        break
    total += int(text)

print(f"Total: {total}")

while True is an intentional infinite loop. The break inside provides the exit. This pattern is readable when the stopping condition is easier to check after reading input.

Do not forget to update the condition

This loop never ends because number stays at 1:

Python
number = 1
while number < 5:
    print(number)

Add number += 1 inside the body.

while versus for#

Use while when the number of iterations is unknown and depends on a condition. Use for when iterating over a sequence or a known range.

Python
# Known count: prefer for
for attempt in range(3):
    print("Attempt", attempt + 1)

# Unknown count: while fits
remaining = 10
while remaining > 0:
    print(remaining)
    remaining -= 3

Validating input#

Python
text = input("Enter a positive number: ")

while not text.isdigit():
    print("Please use digits only.")
    text = input("Enter a positive number: ")

print("You entered", int(text))

In ILoveCompiler, all input lines are supplied before the run. If the available lines run out, input() raises EOFError, which is a useful signal that the program needs more input.

Practice#

Write a loop that starts at 20 and counts down to 0, printing only even numbers. Then write a loop that repeatedly doubles a starting value of 1 until it exceeds 1000, printing each value.

Previous tutorialPython if, elif, and else StatementsNext tutorialPython for Loops

Related tutorials

beginner

Python for Loops

Iterate over strings, lists, ranges, and other iterables with Python's for loop.

8 min read
beginner

Python break, continue, and Loop else

Control loop execution with break and continue, and learn how the rarely used loop else clause works.

6 min read

On this page

  1. Accumulating a result
  2. Reading until a sentinel
  3. while versus for
  4. Validating input
  5. Practice

Run the examples

Every Python block has a Run in compiler button that opens the code in a new tab.