Python Loops – Things You MUST Know about Loops in Python

When a Python script needs to handle each value in a collection, repeating the same code by hand gets in the way. A loop repeats an indented block, with for taking values one at a time and while repeating as long as a condition is true.

I’ll show how to choose between for and while, then use range to set a count and break or continue to control the loop.

TL;DR: Choose a loop by what controls repetition

Use a for loop to process each value from an iterable, and use a while loop to repeat while a condition is true.

  • Use for item in items when the values to visit are available.
  • Use for number in range(…) for a bounded count.
  • Use while condition when the stopping point depends on changing state.
  • Change the state in a while-loop body so its condition can become false.

What is a loop in Python?

A loop repeats an indented block of code. A Python for statement takes values from an iterable one at a time, while while checks a condition before each pass.

colors = ["red", "green", "blue"]
for color in colors:
    print(color)
red
green
blue

Each pass handles the next value in the list, and the same form iterates over strings or dictionary keys.

What you need before running the examples

These examples use Python 3 syntax and require only the interpreter. Pick the loop from the input the code must process.

TaskLoopReason
Visit each available valueforThe iterable provides the next value
Repeat a known countfor with rangeThe range defines the count
Wait for a condition to changewhileThe condition controls each pass

Prefer direct iteration when you need values rather than their positions. Use an index only when the position itself matters.

How to use Python loops step by step

Run each example as a short script. Compare the loop’s input with its printed output to see what controls the next pass.

Step 1: Iterate over each value with for

Put the iterable after in, then write the repeated work in the indented body.

for name in ["Ada", "Lin"]:
    print(name)
Ada
Lin

The body runs once per value. It does not need a counter to advance through a list.

Step 2: Repeat a bounded number of times with range

range(start, stop) includes the start and excludes the stop, so the stop is one past the final value when counting upward.

for number in range(1, 5):
    print(number)
1
2
3
4

The output ends at 4 because range stops before its second argument. For example, list(range(1, 5)) gives [1, 2, 3, 4].

Step 3: Repeat while a condition remains true

Initialize the state before the loop and update it inside the body so the condition can eventually become false.

count = 0
while count < 3:
    print(count)
    count += 1
0
1
2
Python for and while loops printing their values in a terminal
Executed Python examples print the loop values and ranges shown.

The output stops at 2 because the counter reaches 3 before the next condition check. At that point, the condition is false and the loop ends.

Step 4: Skip or stop iterations with break and continue

continue skips the rest of the current body, while break exits the innermost loop immediately.

for number in range(5):
    if number == 2:
        continue
    if number == 4:
        break
    print(number)
0
1
3

The value 2 is skipped, and the loop ends before printing 4. In a while loop, continue starts the next condition check. Put any required state change before that statement.

Common errors with Python loops

A loop’s ending rule determines whether its optional else block runs and whether the code can get stuck repeating.

  • A while condition that never becomes false keeps repeating. Update the state or provide a deliberate exit with break.
  • Python has no direct do-while statement. Its while loop checks the condition before entering the body, so code that must run first needs a separate initial pass or an explicit break condition.
  • A loop’s else runs when the loop finishes without break. It does not mean that the body ran successfully in every sense, and it is skipped after a break.
  • Nested loops put one loop inside another. A break exits only the innermost loop.

Use the loop else to report a search with no match after the loop completes naturally.

for value in [2, 4, 6]:
    if value == 5:
        break
else:
    print("No match")
No match

The list ends without finding 5, so the loop completes without break and prints the message. Python’s control-flow tutorial describes this behavior.

Conclusion

Reach for for when values or a range define the work, and for while when a condition defines when to stop. That distinction makes the loop’s end visible in the code.

For more detail, read Python’s control-flow tutorial and compound statement reference.

for value in ["known values"]:
    print(value)
known values

Python loops FAQ

These answers cover two common choices that are not obvious from loop syntax alone.

What is the difference between for and while loops in Python?

A for loop gets values from an iterable. A while loop repeats as long as its condition is true.

Does Python have a do-while loop?

Python has no direct do-while syntax. Put a first pass before a while loop or use while True with a clear break condition when the body must run before the condition check.

Does range include its stop value?

No. range includes its start and excludes its stop value.

Pankaj Kumar
Pankaj Kumar

I have been working on Python programming for more than 12 years. At AskPython, I share my learning on Python with other fellow developers.

Articles: 256