Python Control Statements

Difference Between For Loop and While Loop in Python

Learn Difference Between For Loop and While Loop in Python with clear explanations, Python examples, and practical programming guidance.

What is the difference between for and while loops?

Both loops repeat code, but they are designed for different situations. A for loop is usually best when iterating over an iterable or a known sequence. A while loop is useful when repetition depends on a condition that may change during execution.

The Python for loop

A for loop visits each item in an iterable such as a list, string, tuple, dictionary, or range:

for item in iterable:
    # code executed for each item
    print(item)

Python automatically obtains the next item and stops when the iterable is exhausted.

Example: using a for loop

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

This loop has a predictable number of iterations because range(1, 6) produces five values.

The Python while loop

A while loop repeats a block as long as its condition is true:

while condition:
    # code executed while condition is true

The condition is checked before every iteration. The loop variable or other state must be updated so the condition can eventually become false.

Example: using a while loop

number = 1

while number <= 5:
    print(number)
    number += 1
Output
1
2
3
4
5

The counter is updated after every iteration. When it becomes 6, the condition is false and the loop ends.

Differences between for and while

Featurefor loopwhile loop
Basic purposeIterates over an iterable.Repeats while a condition is true.
Best choiceWhen a sequence or collection is available.When the stopping condition is dynamic.
Loop variableUpdated automatically for each item.Usually updated manually inside the loop.
TerminationEnds when the iterable has no more items.Ends when its condition becomes false.
Infinite-loop riskLow when iterating over a finite iterable.Higher if the condition never changes.
Common useProcessing lists, strings, files, and ranges.Waiting, retrying, validating, or polling.

Iterating over a collection

A for loop is concise when every item in a collection must be processed:

languages = ["Python", "Java", "SQL"]

for language in languages:
    print(language)
Output
Python
Java
SQL

Repeat until a condition changes

A while loop is useful when the number of repetitions depends on changing state:

attempt = 1

while attempt <= 3:
    print("Attempt", attempt)
    attempt += 1
Output
Attempt 1
Attempt 2
Attempt 3

Choosing the right loop

  • Choose for when you are visiting each item in a collection or a known range.
  • Choose while when the loop should continue until a condition changes.
  • Use break when a loop should stop early.
  • Use continue when one iteration should be skipped.
  • Always update the state used by a while condition to avoid an accidental infinite loop.

Loop else blocks

Both loop types can have an else block. It runs when the loop finishes normally and does not run when the loop exits through break:

for number in range(3):
    print(number)
else:
    print("Loop completed")
Output
0
1
2
Loop completed

Further Reading

Continue learning with these related Python tutorials:

Continue learning