Repeat work with loops

Use a for loop to process each item in a sequence.

A loop repeats a block of code. A for loop is useful when processing each item in a collection or each value in a known range. The range() function generates a sequence of integers; its ending value is excluded.

Count through a short range

python · static example

for number in range(1, 4):
    print(number)

`range(1, 4)` produces 1, 2, and 3.

Sample output
1
2
3

Labeled sample only — nothing was run on this page.

Case study

Scenario

A student society wants to print a numbered checklist for five event volunteers.

Task

What loop could produce the numbers 1 through 5?

Explanation / example

Use `for number in range(1, 6):` and print `number` inside the indented loop. The stop value 6 is excluded.

Knowledge check

What does `range(1, 4)` produce?

Pick an answer to check.

    Repeat work with loops · Python for Beginners | Nestify Campus