Python has two common kinds of loops: for and while. Both repeat code, so beginners often wonder which one to choose.
You do not need to memorize a complicated rule. Ask one question: are you processing a known group of items, or are you waiting for a condition to change? The first usually calls for for; the second usually calls for while.
Start with Two Problems
First, print every name in a list:
names = ["Amy", "Ben", "Chris"]
for name in names:
print(name)
The items are already known. A for loop naturally means “do this once for each item.”
Now keep asking for a password until it is correct:
password = ""
while password != "python123":
password = input("Password: ")
We do not know how many attempts the user will need. We only know the stopping condition, so while is a better match.
When to Use for
Use for when you want to visit each item in a collection such as a list, string, or tuple.
scores = [82, 91, 76]
for score in scores:
print(score)
If the number of repetitions is known, use for with range():
for number in range(5):
print(number)
A for loop manages the next item for you, which usually makes the code shorter and reduces the chance of forgetting an update.
When to Use while
Use while when the number of repetitions is unknown but the condition for continuing is clear.
answer = ""
while answer != "quit":
answer = input("Type quit to stop: ").lower()
This loop may run once or ten times. It continues because answer != "quit" is true, not because a fixed number of repetitions was chosen in advance.
Common uses include user input, game states, simulations, and tasks described as “keep going until…”
Could Both Loops Work?
Often, yes, but one version may express the idea more clearly. Here is a while loop that prints 0 through 4:
number = 0
while number < 5:
print(number)
number += 1
You must initialize the counter, test it, and update it. If you forget number += 1, the loop may never stop.
The for version is more direct:
for number in range(5):
print(number)
When the task naturally sounds like “for each item” or “repeat this many times,” prefer for.
The Most Important while Check
Inside a while loop, something must move the condition toward False.
count = 1
while count <= 3:
print(count)
count += 1
Here, count += 1 eventually makes count <= 3 false. Without that update, the loop would not end.
After writing a while loop, ask: which variable controls the loop, where does it change, and when will the condition become false?
Quick Comparison
| Situation | Recommended loop | Clue |
|---|---|---|
| Process every item in a list or string | for |
Once for each item |
| Repeat a known number of times | for + range() |
The count is known |
| Wait for specific user input | while |
Unknown count, clear stop condition |
| Continue while a state is true | while |
The condition controls repetition |
This is a practical guideline, not an unbreakable law. Choose the loop that describes the problem most clearly.
Practice
- Print every color in
colors = ["red", "green", "blue"]. Which loop should you choose? - Read user input until the user enters a blank line. Which loop should you choose?
- Challenge: print 1 through 5 with both
forandwhile, then compare the steps each version requires.
One-Sentence Takeaway
Use for for known items or a known count; use while when repetition depends on a changing condition and the count is unknown.
Questions or feedback?
Have a question about this article or want to suggest an improvement? Send us an email.
