for Loop (Python)

A Python for loop repeats a block of code once for each item in a sequence such as a list, string, or range. It is the main way to iterate in Python.

Python

Syntax

for item in iterable:
    # do something

Examples

Python

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

Output:

apple
pear

Python

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

Output:

0
1
2

Learn it in a lesson

See also

Back to the Dictionary