PYTHON CORE · LESSON 08

Repeats

for loop · about 12 minutes

Learning with progressRead the summary for free. To save the work, log in to the account.
STEP 1

Don't write the same thing many times.

The for loop repeats the command. range(3) will give the number 0, 1, 2 – just three steps.

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

✦ The line within the loop starts with four spaces.

STEP 2

Choose a beginning and an end

In range(2, 5), the first number is the beginning. The number 5 is the boundary, the loop will reach it, but 5 itself will not take.

for number in range(2, 5):
    print(number)

✦ It's going to be 2, 3, 4.

IN-CLASS PRACTICE

Type the code yourself

Dial the loop. Find colon and indentation in front of print.

for count in range(3):
    print(count)
Open the editor for class work
HOMEWORK

Solve it yourself

Using the for and range loop, output the numbers 1, 2, 3. Name the loop variable step.

The answer is hidden. Use similar examples from the lesson notes.

Opening up your homework