Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
How many numbers does each row have, and which is the last number? Say it first, then run a program to check.
range(7)range(3, 12)range(0, 20, 5)range(10, 0, -2)Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. The stop value is never included. For a step of 1, range(a, b) has b - a numbers.
range(7): 0, 1, 2, 3, 4, 5, 6. Seven numbers, the last is 6.range(3, 12): 3 to 11. 12 - 3 = 9 numbers, the last is 11.range(0, 20, 5): 0, 5, 10, 15. Four numbers, the last is 15 (20 is the stop and is not included).range(10, 0, -2): 10, 8, 6, 4, 2. Five numbers, the last is 2 (it stops before 0).Check. Run print(list(range(10, 0, -2))) and compare with your answer for each row.
Answer: 7 numbers (last 6); 9 numbers (last 11); 4 numbers (last 15); 5 numbers (last 2).
Log in to join the discussion.
Loading…