The for loop is one of the first things you need to understand in Python. It gives you the ability to repeat an action for every item in a collection, and it forms the foundation of nearly every real Python program you will write.
When you are new to Python, loops can seem abstract. This tutorial builds your understanding from the ground up — starting with what a loop actually is, moving through the most common patterns you will use every day, and finishing with interactive challenges to test what you have learned.
What Is a for Loop?
A for loop tells Python: "go through this collection of items, and for each one, do this." The loop body — the indented code beneath the for statement — runs once per item. When the collection runs out of items, the loop ends and Python continues with the rest of your program.
The basic structure looks like this:
for item in collection:
# this block runs once per item
print(item)
Three things matter here. First, the word for starts the statement. Second, you choose a name for the loop variable — item in the example above — which Python will automatically update on each pass to hold the current value from the collection. Third, the colon at the end of the for line signals that an indented block follows.
Indentation is not optional in Python. Every line inside the loop body must be indented by the same amount — conventionally four spaces. A missing or inconsistent indent will cause an IndentationError and your program will not run.
The word you choose for the loop variable does not matter to Python — it only matters to you and anyone else reading your code. Pick a name that describes what each item represents. If you are looping over a list of usernames, call it username. If you are looping over a list of prices, call it price.
colors = ["red", "green", "blue"]
for color in colors:
print(color)
# Output:
# red
# green
# blue
Python reads this as: "for each color in the colors list, print that color." The list has three items, so the print statement runs exactly three times. The variable color holds "red" on the first pass, "green" on the second, and "blue" on the third.
Build a for loop that prints each fruit in a list called fruits:
for, then the loop variable name (fruit), then in, then the iterable (fruits), then a colon. Python does not use while or of — those are not valid for-loop keywords. The indented body print(fruit) runs once per item.
Iterating Over Lists and Strings
Python's for loop works on any object that is iterable — meaning any object Python knows how to step through one item at a time. Lists and strings are both iterable, and they are the two types you will use most often as a beginner.
Looping over a list
A list holds multiple values in a single variable. The for loop visits each value in the order it was added to the list.
scores = [88, 72, 95, 60]
for score in scores:
print("Score:", score)
# Output:
# Score: 88
# Score: 72
# Score: 95
# Score: 60
Looping over a string
A string is a sequence of characters, and Python treats each character as an individual item when you loop over it.
word = "Python"
for letter in word:
print(letter)
# Output:
# P
# y
# t
# h
# o
# n
You can loop over other sequence types too — tuples, sets, and the keys of a dictionary are all iterable. The same for item in collection: syntax applies to all of them.
Using break and continue
Two keywords let you control what happens inside the loop. break exits the loop immediately. continue skips the rest of the current iteration and moves straight to the next item.
numbers = [1, 2, 3, 4, 5]
# break — stop the loop when we hit 4
for n in numbers:
if n == 4:
break
print(n)
# Output: 1 2 3
# continue — skip even numbers
for n in numbers:
if n % 2 == 0:
continue
print(n)
# Output: 1 3 5
- What it does
- Exits the loop immediately. No remaining items are processed.
- When to use
- When you have found what you were looking for and no longer need to continue iterating.
- What it does
- Skips the rest of the current iteration and moves to the next item.
- When to use
- When you want to filter out certain items without stopping the loop entirely.
- What it does
- Does nothing — it is a placeholder that lets the loop body exist without any actual code.
- When to use
- When you need a syntactically complete loop but have not yet written the body. Useful while building code incrementally.
This loop is supposed to print each name in a list. One line contains a bug. Click the line you think is wrong, then hit check.
of to in. Python's for loop uses the keyword in — not of, which is used in JavaScript. The correct line is for name in names:
Using range() With a for Loop
range() is a built-in Python function that generates a sequence of numbers. It is the most common way to run a loop a specific number of times when you do not have a list to iterate over.
# range(stop) — generates 0, 1, 2, ... stop-1
for i in range(5):
print(i)
# Output: 0 1 2 3 4
# range(start, stop) — starts at start, stops before stop
for i in range(1, 6):
print(i)
# Output: 1 2 3 4 5
# range(start, stop, step) — counts by step
for i in range(0, 10, 2):
print(i)
# Output: 0 2 4 6 8
range(5) produces the numbers 0 through 4, not 1 through 5. The stop value is always excluded. This is a common source of off-by-one errors for beginners.
You can also use range() to loop backwards by using a negative step value:
for i in range(5, 0, -1):
print(i)
# Output: 5 4 3 2 1
Nested for loops
You can place a for loop inside another for loop. The inner loop runs its full cycle for every single iteration of the outer loop. This pattern is common when working with grids or tables.
for row in range(1, 4):
for col in range(1, 4):
print(f"row {row}, col {col}")
# Output:
# row 1, col 1
# row 1, col 2
# row 1, col 3
# row 2, col 1
# ... and so on
How to Write a for Loop in Python
Follow these four steps any time you need to write a new for loop from scratch.
-
Choose your iterable
Identify the sequence you want to loop over. This could be a list, a string, a tuple, or a call to
range(). If you want to run a loop a fixed number of times, userange(n). If you have a collection of values, use the collection directly. -
Write the for statement
Type the
forkeyword, choose a descriptive loop variable name, then writeinfollowed by your iterable and a colon. For example:for item in my_list:. The loop variable name is yours to choose — make it meaningful. -
Indent the loop body
Press Tab or add four spaces on the next line and write the code you want to run on each iteration. Every line that belongs to the loop must be consistently indented. Python uses indentation — not braces — to define scope.
-
Run and verify output
Execute your script. The loop body will run once per item. Use
print()to inspect the loop variable on each pass until you are confident the loop is working as intended. Reading the output line by line is the clearest way to understand what the loop is doing.
"Explicit is better than implicit." — The Zen of Python, PEP 20
Python Learning Summary Points
- The
forloop iterates over any iterable in Python — lists, strings, tuples, ranges, and more. The loop body runs once per item, and Python moves on when all items have been processed. range()is the standard way to run a loop a specific number of times. Remember that the stop value is excluded:range(5)produces 0, 1, 2, 3, 4 — not 1 through 5.- Indentation defines the loop body in Python. Every line you want to execute on each iteration must be indented by the same number of spaces. This is not optional — it is how Python reads the structure of your code.
breakexits the loop immediately.continueskips the rest of the current iteration. Both keywords give you fine-grained control over loop behavior without requiring you to restructure your code.- Nested
forloops let you iterate over multi-dimensional data. The inner loop completes its full run for every single iteration of the outer loop — keep this in mind to avoid writing loops that do far more work than you intended.
The for loop is one of the most-used constructs in Python. Once it feels natural, you will reach for it constantly — to process files, transform data, validate input, and build logic of every kind. Practice by writing small loops on your own and reading the output carefully until the pattern is second nature.
Frequently Asked Questions
The for loop in Python iterates over the items in any sequence or iterable object — such as a list, string, tuple, or range — executing the indented block of code once for each item.
The basic syntax is for variable in iterable: followed by an indented block. For example: for name in names: print(name). The colon and indentation are both required.
range() generates a sequence of numbers. range(5) produces 0, 1, 2, 3, 4. range(start, stop) and range(start, stop, step) give you more control. The for loop iterates over each number that range() produces.
A loop variable is the name you choose in the for statement that holds the current item on each pass through the loop. You can name it anything, but it should clearly describe what it represents — for example, name when looping over a list of names.
Yes. A string is an iterable in Python, so a for loop will step through each character one at a time. for letter in "Python": would print P, y, t, h, o, n on separate lines.
A nested for loop is a for loop placed inside another for loop. The inner loop runs its full cycle for every single iteration of the outer loop. This pattern is useful for working with grids, tables, and multi-dimensional data.
A for loop is designed for iterating over a known sequence or iterable. A while loop continues as long as a condition is True and is better suited for situations where you do not know in advance how many times the loop will run.
break exits the for loop immediately, skipping any remaining iterations, even if there are items left in the sequence. Use it when you have found what you need and further iteration would be wasted work.
continue skips the rest of the current iteration and moves directly to the next item in the sequence, without stopping the loop entirely. It is useful when you want to filter out certain items inside the loop.
Yes. Python uses indentation to define the body of the loop. Any line that is indented consistently under the for statement is treated as part of the loop body. Inconsistent or missing indentation will cause an IndentationError and your program will not run.