Skip to the content

for and range

Python · Unit 3: Loops

for repeats a block of code. range(3) says how many times: three.

Like with if, the block is whatever is indented after the colon.

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

Prints

Hello
Hello
Hello

The rest of the explanation is in the lesson, which is part of the full plan.

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Predict the output

    What does this code print?

  2. 2. Predict the output

    What does this code print?

  3. 3. Multiple choice

    How many times does the block of for i in range(5): repeat?

  4. 4. Complete the code

    Complete this to print the numbers from 1 to 10.

  5. 5. Predict the output

    What does this code print?

  6. 6. Predict the output

    What does this code print?

  7. 7. Complete the code

    Complete the countdown so it prints 5, 4, 3, 2, 1 and lifts off.

  8. 8. Find the bug

    This program should print the 7 times table, from 7 x 1 to 7 x 10. Which line has the error?

  9. 9. Put the lines in order

    Put the lines in order to print the even numbers from 1 to 6.

  10. 10. Multiple choice

    What does this code do?

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →