SythraOpen app

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.

Sythra

7 min read

XLinkedIn
The for Loop in Python — cover illustration

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
# 5

What you will learn

  • How a for loop 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 for instead of while

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 it i for 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
# t

Or loop over items inside a list:

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

# Output:
# apple
# banana
# mango

range() 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
# 8

The 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
# 1

This 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 while with a manual counter when a plain for 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.

Browse all python explainers →