Skip to content
beginner

For Loops in Python

A for loop is how Python repeats an action for every item in a collection. You describe the action once, and Python runs it for each value in turn. That…

Published 2026-05-11Updated 2026-09-157 min read
High-tech laboratory equipment with computer system in lab setting.
High-tech laboratory equipment with computer system in lab setting. Photo by Media Dung on Pexels.

A for loop is how Python repeats an action for every item in a collection. You describe the action once, and Python runs it for each value in turn. That one idea turns a pile of copy-pasted lines into a few lines that scale with your data.

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)
apple
banana
cherry

Watch what just happened. Python took the first value, "apple", handed it to the name fruit, ran the indented print, then moved to the next value and did it again. That is the whole mechanism of a python for loop: one value arrives, one body runs, one output appears, and the loop advances until the collection runs out.

What a For Loop Is

A for loop repeats an action for each item in a collection. Read it as: "For every item in this group, run this block of code."

Here is what happens on each pass, step by step:

  • fruit = "apple", then print(fruit) runs.
  • fruit = "banana", then print(fruit) runs again.
  • fruit = "cherry", then print(fruit) runs one last time.

The same variable name, fruit, is rebound to one current value on each iteration. It does not accumulate the items, and it is not a position number. It is a fresh label handed the next value in line. When the loop ends, fruit keeps the last value it was given.

The variable name is your choice. fruit reads well here, but item, name, or x all work. What matters is that the loop hands you each value in turn, and the indented block runs once per value.

This process of moving through a collection one item at a time is called iteration, and the for loop is Python's main tool for it. If lists or variables are still unfamiliar, review those fundamentals before continuing.

Knowledge check

Check your understanding

Answer this question before you continue.

In `for fruit in fruits:`, what does `fruit` represent on each iteration?
Misconception Check

Focus: Distinguish the values supplied by a Python for loop from their positions.

Choose What Supplies Each Iteration

Three-column comparison showing direct values from a collection, values paired with positions by enumerate(), and generated numbers from range(), each feeding a for-loop body.
Choose the loop source based on what each iteration needs: values, values plus positions, or generated numbers.

Here is the decision that organizes every loop you will write: decide what should supply each iteration. You have three common answers, and each maps to a different tool.

  • The values themselves — loop directly over a list, string, or other collection.
  • Values plus their positions — use enumerate().
  • Generated numbers — use range().

Keep that lens in mind as we walk through each one. It turns the rest of this lesson from a list of features into one repeatable choice.

Loop Over the Values Directly

A for loop works on any iterable—a value that can hand you its items one at a time. The most common ones for beginners are:

  • Lists: each element in order.
  • Strings: each character in order.
  • Ranges: a sequence of numbers.
  • Tuples: each item in order.
word = "code"
for letter in word:
    print(letter)
c
o
d
e

Notice that a string behaves like a list of its characters. That is a useful mental model: if a value can be presented as an ordered set of items, a for loop can walk through it.

Loop Over Values Plus Positions with enumerate()

When you need the position as well as the value, ask for both. enumerate() hands you each item paired with its index, starting at zero:

fruits = ["apple", "banana", "cherry"]
for index, fruit in enumerate(fruits):
    print(index, fruit)
0 apple
1 banana
2 cherry

This trips up people coming from other languages, and it is worth catching early: Python's for loop is a "for each" loop, not a counter loop. It gives you the values themselves. If you genuinely need the position too, reach for enumerate() rather than trying to count by hand.

Knowledge check

Check your understanding

Answer this question before you continue.

Which loop correctly gives both the position and the fruit for each item in `fruits`?
Single Choice

Focus: Choose enumerate() when a loop needs both each value and its position.

`fruits = ["apple", "banana"]`

Loop Over Generated Numbers with range()

Sometimes you do not have a list to loop over—you just want to repeat an action a set number of times. That is what range() is for.

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

range(1, 6) produces the numbers 1 through 5. The end value is excluded, so range(1, 6) stops before 6. If you want to start at zero, range(5) gives you 0, 1, 2, 3, 4.

A common beginner trap is assuming range is part of the for-loop syntax. It is not. It is a built-in function that returns a sequence of numbers, and the for loop just iterates over that sequence like any other collection.

Knowledge check

Check your understanding

Answer this question before you continue.

What does this code print?
Output Prediction

Focus: Predict the values produced by range() when its end value is excluded.

```python
for number in range(1, 4):
    print(number)
```

For Loops vs. While Loops

Python has two main ways to repeat actions, and they answer different questions.

LoopBest whenExample
forYou know the items or the count up frontLoop through every name in a list
whileYou keep going until a condition changesKeep asking for input until the user types quit

A for loop is the right tool when you want to visit every item in a collection or repeat something a known number of times. A while loop is the right tool when the stopping point depends on something that changes while the loop runs. If you are not sure which you need, start with the for loop—it is the one beginners reach for most, and it is harder to write an accidental infinite loop with it.

Common Beginner Mistakes

Forgetting the indentation. The indented block is the body of the loop. If you forget to indent, Python raises an IndentationError because it cannot tell which lines belong inside the loop.

## Wrong: the print is not inside the loop
for fruit in fruits:
print(fruit)
## Right: the print is indented
for fruit in fruits:
    print(fruit)

Looping over the wrong thing. If you try to loop over a single number instead of a collection, Python will complain. Loop over a list, string, or range(), not over a bare integer.

Expecting an index when you get a value. In many languages, a loop gives you a counter. In Python, for fruit in fruits gives you the values themselves. If you genuinely need the position too, use enumerate() as shown above.

Knowledge check

Check your understanding

Answer this question before you continue.

What change fixes this code so `print(fruit)` is the loop body?
Debugging

Focus: Identify missing indentation as the cause of a for-loop body error.

```python
fruits = ["apple", "banana"]
for fruit in fruits:
print(fruit)
```

A Real-World Use: Processing Data

Loops become valuable the moment you have data to process. Say you have a list of scores and you want to total them:

scores = [82, 91, 77, 88]
total = 0
for score in scores:
    total += score
print(total)
338

The loop visits each score, adds it to total, and moves on. If the list grows to 10,000 scores, the loop still works—you wrote the action once, and Python repeats it for every item. That is the leverage a loop gives you: one small block of code that scales with your data.

Practice: Make It Stick

The fastest way to learn a loop is to run one, change it, and watch what happens. Start with the smallest observable edit: take the scores example and add a count variable that starts at 0 before the loop, then add count += 1 inside the loop, and print count after it.

scores = [82, 91, 77, 88]
total = 0
count = 0
for score in scores:
    total += score
    count += 1
print(total)
print(count)
338
4

You should see 4 for the four scores. That one change confirms the mental model: the loop body ran once per item, and count proves it. Once that lands, try a few more on your own:

  • Make a list of your favorite foods and print each one with a for loop.
  • Use range() to print the numbers 1 to 10.
  • Loop through the letters in your name and print each letter on its own line.

Here is the decision rule that ties the whole lesson together: iterate directly over values when you need the values; use enumerate() when you also need the positions; use range() when the numbers themselves are the iterable. Once you are comfortable moving through collections, practice controlling the flow inside a loop by stopping early or skipping items. That is where loops stop being a simple repeat and start becoming real logic.

Knowledge check

Final check

Finish the article by checking the ideas you just learned.

What does this code print?
Question 1 of 2Output Prediction

Focus: Trace a for loop that accumulates values into a running total.

```python
scores = [10, 20, 5]
total = 0
for score in scores:
    total += score
print(total)
```
Which tool should you use when you need to repeat an action for the numbers 1 through 5?
Question 2 of 2Single Choice

Focus: Select for, enumerate(), or range() based on whether a task needs values, positions, or generated numbers.

References

  1. Python for Loops: The Pythonic Way – Real Pythonrealpython.com
  2. Python for Loop (With Code Visualization)www.programiz.com
  3. ForLoopwiki.python.org
Practical resource

Want a more structured Python path?

Use the Python Starter Pack to turn scattered tutorials into a focused practice path.

View the bundle
Coming soon

Python Starter Pack

A compact LearnPyFast PDF pack covering what Python is, installation, your first program, running Python code, and Python versions.

$9
PDF BundleTopic PackPythonBeginner
  • 5 curated chapters
  • Enhanced PDF edition with bundle-only learning guidance
  • Offline-friendly format for focused review
  • Source article links for future online updates

Coming soon

Free Python bundle

Get the LearnPyFast Python for Artificial Intelligence Starter Bundle

Build a Python foundation you can actually use. The Python for Artificial Intelligence Starter Pack brings together a guided path through setup, core programming concepts, data structures, files, JSON, APIs, debugging, and practical projects—so you can move quickly from running your first program to understanding and building useful software.

You’ll receive the bundle by email. You can unsubscribe anytime.

No spam. You can unsubscribe anytime. See our Privacy policy.

Related sites

Continue beyond Python

Explore related Worldmonger sites when you want to move from Python basics into JavaScript or LLM application building.

JavaScript tutorialstutorial

LearnJSFast

Beginner-friendly JavaScript tutorials for practical web development and self-taught developers.

JavaScriptFrontendWeb development
Visit LearnJSFast
LLM tutorialstutorial

LearnLLMFast

Practical LLM tutorials for builders who want to understand prompting, workflows, agents, and AI applications.

LLMAIBuilders
Visit LearnLLMFast

Keep learning

Related tutorials

Continue with nearby Python topics and beginner-friendly explanations.

Two programmers working together with focus on coding in a modern, tech-savvy office environment.
beginner
8 min read

If Statements in Python

An if statement is how a Python program turns a condition into a decision: check something, then run one block of code or skip it. The whole skill is…

Read tutorial