Quick Answer

A Python for loop repeats a block of code once for each item in a sequence, such as a list, a string, a range of numbers, or a dictionary. You write "for item in sequence:" and indent the code you want to repeat. It is the standard, readable way to process collections in Python without copying the same line over and over.

What is a for loop in Python?

A Python for loop lets you run the same block of code once for every item in a sequence. Instead of writing the same line five times, you write it once and let the loop repeat it. The sequence can be a list, a string, a range of numbers, a dictionary, or anything Python can step through one item at a time (these are called iterables).

Here is the idea in one small example:

for fruit in ["apple", "banana", "mango"]:
    print(fruit)

# Output:
# apple
# banana
# mango

On each pass, Python takes the next item from the list, stores it in the variable fruit, and runs the indented line. When the list runs out, the loop stops. That is the whole concept. The rest of this tutorial just shows the useful patterns you will reach for every day. Want a structured path with exercises and projects? Our free Python course covers loops and much more from scratch.

The basic syntax

Every for loop has the same shape:

for variable in sequence:
    # indented body runs once per item
    do_something(variable)

Three things to remember:

  • The line ends with a colon (:). Forgetting it is the most common beginner error.
  • The body is indented (4 spaces is the standard). Indentation is how Python knows which lines belong to the loop.
  • The variable name is yours to choose. Pick something clear like student or price, not just x.

A quick real-world example: printing a total for each item in a shopping cart.

cart = [199, 349, 99]
total = 0
for price in cart:
    total = total + price
print("Total:", total)

# Output:
# Total: 647

Notice how total lives outside the loop, so it keeps its value across every pass and adds up correctly.

Looping with range(): 1, 2, and 3 arguments

Very often you want to repeat something a fixed number of times, or count through numbers. The built-in range() function is made for this. It comes in three forms.

One argument: range(stop)

Counts from 0 up to (but not including) the stop number.

for i in range(5):
    print(i)

# Output: 0 1 2 3 4 (each on its own line)

Two arguments: range(start, stop)

Counts from start up to, but not including, stop.

for i in range(2, 6):
    print(i)

# Output: 2 3 4 5

Three arguments: range(start, stop, step)

The third number is the step (how much to jump each time). A negative step counts backwards.

for i in range(1, 10, 2):
    print(i)
# Output: 1 3 5 7 9

for i in range(10, 0, -2):
    print(i)
# Output: 10 8 6 4 2

The big gotcha: the stop value is always excluded. So range(1, 5) gives you 1, 2, 3, 4 — never 5. If you want the numbers 1 through 5, write range(1, 6).

Looping over a list and a string

You do not need range() to walk through a collection. A for loop can step directly over the items, which is cleaner and easier to read.

Looping over a list

students = ["Aisha", "Ravi", "Meena"]
for name in students:
    print("Hello,", name)

# Output:
# Hello, Aisha
# Hello, Ravi
# Hello, Meena

Looping over a string

A string is a sequence of characters, so a for loop gives you one character at a time.

for letter in "Python":
    print(letter)

# Output: P y t h o n (each on its own line)

This is handy for tasks like counting a specific character:

word = "banana"
count = 0
for letter in word:
    if letter == "a":
        count = count + 1
print(count)   # Output: 3

Looping over a dictionary with .items()

Dictionaries store key–value pairs. When you loop directly over a dictionary, you only get the keys:

marks = {"Aisha": 92, "Ravi": 85}
for name in marks:
    print(name)

# Output:
# Aisha
# Ravi

Most of the time you want the key and the value together. Use .items(), which hands you both on each pass:

marks = {"Aisha": 92, "Ravi": 85}
for name, score in marks.items():
    print(name, "scored", score)

# Output:
# Aisha scored 92
# Ravi scored 85

Here name receives the key and score receives the value. This trick of unpacking two variables at once is called tuple unpacking, and you will see it again with enumerate() below. If you only need the values, there is also .values().

Getting the index too with enumerate()

Sometimes you need the item and its position number. Beginners often reach for range(len(...)), but there is a cleaner, more Pythonic way: enumerate().

subjects = ["Math", "Physics", "Chemistry"]
for index, subject in enumerate(subjects):
    print(index, subject)

# Output:
# 0 Math
# 1 Physics
# 2 Chemistry

By default the count starts at 0. To start from 1 (nicer for numbered lists shown to people), pass start=1:

for index, subject in enumerate(subjects, start=1):
    print(index, subject)

# Output:
# 1 Math
# 2 Physics
# 3 Chemistry

Recommendation: whenever you catch yourself writing for i in range(len(my_list)) just to read my_list[i], switch to enumerate(). It is shorter and avoids off-by-one mistakes.

Nested loops

A loop can live inside another loop. For every single pass of the outer loop, the inner loop runs all the way through. This is perfect for grids, tables, and combinations.

for i in range(1, 4):
    for j in range(1, 4):
        print(i * j, end=" ")
    print()   # move to a new line after each row

# Output:
# 1 2 3
# 2 4 6
# 3 6 9

The inner print(i * j, end=" ") keeps numbers on the same line, and the outer print() (with no arguments) drops to the next line after each row. That is a tiny multiplication table.

Gotcha to watch: nested loops multiply the work. Two loops of 1,000 items each do a million passes. That is fine for small data, but be careful before nesting loops over very large collections.

Controlling the loop: break and continue

Two keywords let you change how a loop flows.

break — stop the loop early

break exits the loop immediately, skipping any remaining items. Great for stopping as soon as you find what you were looking for.

for number in range(1, 10):
    if number == 5:
        break
    print(number)

# Output: 1 2 3 4

continue — skip to the next item

continue jumps straight to the next pass without running the rest of the body. The loop itself keeps going.

for number in range(1, 6):
    if number == 3:
        continue
    print(number)

# Output: 1 2 4 5

Read the difference like this: break means "I'm done, leave the loop," while continue means "skip just this one, carry on."

The for...else clause

Python has one feature that surprises almost everyone: a for loop can have an else block. The else runs only if the loop finished without hitting a break. It is designed for search problems.

names = ["Ravi", "Priya", "Arjun"]
search = "Meena"

for name in names:
    if name == search:
        print("Found", search)
        break
else:
    print(search, "is not in the list")

# Output:
# Meena is not in the list

Because Meena is never found, the loop never breaks, so the else runs. If you change search to "Priya", the loop breaks on a match and the else is skipped.

Recommendation: for...else is genuinely useful, but many beginners find it confusing to read. If a teammate might be puzzled, a plain "found" flag variable is a fine, clearer alternative. Once you are comfortable, though, it removes some boilerplate. Practice all of these patterns hands-on in our free Python course.

Frequently Asked Questions

What is the difference between a for loop and a while loop?

A for loop runs once for each item in a known sequence (a list, string, range, and so on), so you use it when you know what you are iterating over. A while loop keeps running as long as a condition stays true, so you use it when you do not know the number of repetitions in advance, such as waiting for valid user input.

Why does range(5) stop at 4 and not 5?

The stop value in range() is exclusive, meaning it is never included. So range(5) produces 0, 1, 2, 3, 4 — five numbers starting from 0. If you want the numbers 1 through 5, write range(1, 6).

How do I loop over a dictionary's keys and values together?

Use the .items() method and unpack two variables: for key, value in my_dict.items():. This gives you both the key and its value on every pass. Looping over the dictionary directly gives only the keys.

When should I use enumerate() instead of range(len())?

Use enumerate() whenever you need both the position and the item. Writing for i in range(len(items)) just to access items[i] is longer and more error-prone. for i, item in enumerate(items) gives you both cleanly, and you can pass start=1 to begin counting from 1.

What is the difference between break and continue?

break stops the entire loop immediately and moves on to the code after it. continue skips only the current pass and jumps to the next item, so the loop keeps running. Think of break as "leave the loop" and continue as "skip this one."

When does the else block of a for loop run?

The else block after a for loop runs only if the loop completed normally, without a break being triggered. It is most useful in search loops, where the else handles the "not found" case. If the loop breaks early, the else is skipped.