Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

for

Use for ... in ... to go through each value in something like a string, tuple, or range(...).

actor main(env):
    names = ("Ada", "Grace", "Linus")

    for name in names:
        print("Hello", name)

    for n in range(3):
        print("n =", n)

    env.exit(0)

A for loop is the usual choice when you want to go through each item in a collection or repeat something a known number of times. The loop variable is a new local name that takes on each value in turn.

range(stop) counts from 0 up to, but not including, stop.

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

You can also use range(start, stop, step).

for n in range(2, 10, 2):
    print(n)

The loop variable is local to the loop body. A common pattern is to use for with a collection when the values matter, and range(...) when the count matters.

In Acton, for is usually the clearest way to consume an iterable because it avoids manual index state and keeps the element type front and center. When you need both the index and the value, prefer enumerate(...) over range(...). Use range(...) when the numbers themselves matter, not as a default substitute for iterating a collection.

for i, name in enumerate(names):
    print(i, name)