Counting steps
Python · Unit 22: Complexity
- Full plan
- Python
- 9 exercises
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. Predict the output
What does this code print?
2. Complete the code
Complete it so
stepsends up saying how many rounds the loop made.3. Predict the output
What does this code print?
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. Complete the code
Complete it so the loop makes half as many rounds.
6. Predict the output
What does this code print?
7. Find the bug
It should say how many rounds the loop made, that is 4. Which line has the error?
8. Put the lines in order
Put together a function that counts how many numbers a table of rows holds in total.
9. Predict the output
What does this code print?
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.