Skip to the content

Counting steps

Python · Unit 22: Complexity

To know how much work a program does you don't look at the clock: you count the steps.

A variable that goes up by 1 inside the loop tells you how many times it went around.

steps = 0
for n in [4, 7, 1]:
    steps += 1
print(steps)

Prints

3

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. Complete the code

    Complete it so steps ends up saying how many rounds the loop made.

  3. 3. Predict the output

    What does this code print?

  4. 4. Multiple choice

    A function makes 20 steps with n = 10 and 200 steps with n = 100. How many will it make with n = 1000?

  5. 5. Complete the code

    Complete it so the loop makes half as many rounds.

  6. 6. Predict the output

    What does this code print?

  7. 7. Find the bug

    It should say how many rounds the loop made, that is 4. Which line has the error?

  8. 8. Put the lines in order

    Put together a function that counts how many numbers a table of rows holds in total.

  9. 9. Predict the output

    What does this code print?

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 →