for loop

A loop that runs once for each item in a sequence.

A for loop walks through a sequence — a list, string, range, dictionary, or any iterable — binding each item to a variable in turn and running its body once per item. It is the go-to loop when you know what you are iterating over.

Example
for fruit in ["apple", "pear", "plum"]:
    print(fruit.upper())

for i in range(3):        # 0, 1, 2
    print("row", i)
Output
APPLE
PEAR
PLUM
row 0
row 1
row 2

Where this shows up in real Python

For loops process each file in a folder, each row in a CSV, each result from an API — anywhere you handle a collection one item at a time.

Common for-loop tools

  • range(n) — loop a fixed number of times
  • enumerate(seq) — loop with an index
  • zip(a, b) — loop two sequences together
  • break / continue — stop early / skip an item
  • for k, v in d.items() — loop a dictionary’s pairs

Official documentation: Python Tutorial: for Statements

Related lessons

Related terms