The for Loop in Python
A for loop in Python pulls values one at a time from a sequence — like range(), a string, or a list — and runs its body once per value, stopping automatically when the sequence is exhausted.
The while loop is great when you don't know in advance how many times you'll repeat — you just keep going until some condition changes. But very often, you already know exactly what you want to repeat over — like "every number from 1 to 10" or "every letter in this word." For that, Python gives you something more direct: the for loop.
for i in range(1, 6):
print(i)
# Output:
# 1
# 2
# 3
# 4
# 5What you will learn
- How a
forloop pulls values one at a time from a sequence - Why
range(1, 6)stops before 6, not at 6 - Looping over strings and lists directly
- How to control start, stop, and step with
range() - When to reach for
forinstead ofwhile
Breaking down for i in range(1, 6):
range(1, 6)— creates a sequence of numbers starting at 1, and stopping before 6. So you get 1, 2, 3, 4, 5 — five numbers, not six. This "stop before, not at" behavior catches almost every beginner at least once, so keep an eye on it.for i in ...— this means: one at a time, take each value out of that sequence, call itifor this trip through the loop, and run the body.- The loop automatically runs once for every single item in the sequence, and then stops on its own — no
break, no condition to write yourself, no risk of an infinite loop by accident.
Looping over strings and lists
You can also loop over the actual characters in a piece of text:
word = "cat"
for letter in word:
print(letter)
# Output:
# c
# a
# tOr loop over items inside a list:
fruits = ["apple", "banana", "mango"]
for fruit in fruits:
print(fruit)
# Output:
# apple
# banana
# mangorange() with a start, stop, and step
range() actually accepts up to three numbers — a start, a stop, and a step:
for i in range(0, 10, 2):
print(i)
# Output:
# 0
# 2
# 4
# 6
# 8The third number (2 here) is the step — how much to jump by on each repeat. If you leave it out, Python assumes a step of 1, like in the very first example. You can even count backwards using a negative step:
for i in range(5, 0, -1):
print(i)
# Output:
# 5
# 4
# 3
# 2
# 1This is actually a cleaner way to write the countdown function from the while loop article — no manual decrement needed.
Common mistakes
- Expecting
range(1, 6)to include 6 — it stops before the stop value - Reaching for
whilewith a manual counter when a plainfor i in range(...)would be shorter and safer - Forgetting that a negative step is required to count downward with
range() - Trying to modify a list while looping over it directly with
for— this can skip items unpredictably
Common questions
Why doesn't range(1, 6) include 6?
range() in Python always stops before its stop value, never at it. range(1, 6) produces 1, 2, 3, 4, 5 — five numbers. This half-open behavior is consistent across Python's slicing and indexing too.
Can a for loop iterate over a string?
Yes. A string is a sequence of characters, so for letter in word: loops over each character one at a time, in order.
What are the three arguments to range()?
range(start, stop, step): start is the first number (default 0), stop is where it stops before, and step is how much to increase by each time (default 1). A negative step counts downward.
Should I use for or while in Python?
Use for when you already know what you're iterating over — a range of numbers, characters in a string, or items in a list. Use while when you're repeating until some condition changes, and you don't know the number of repeats in advance.
Explore
Related topics
Keep going — these sit next to this concept in a real learning path.
The while Loop in Python
Condition-based repetition — the loop for when you don't know the count in advance.
The do-while Equivalent in Python (and Nested Loops)
Simulating run-at-least-once loops and nesting loops.
More Tools for Iteration in Python
continue, enumerate(), zip(), comprehensions, and generators.
Lists in Python
The sequence type you'll loop over most often.
Python course hub
All free Python explainers and the path into Agentic practice.