+ 1
Why choose to code range(n,m) as stopping at m-1?
2 Respostas
+ 5
I guess either way you do it is ok, but there are a few things to be said about stopping at m-1.
In the most common case (n == 0) stopping at m-1 has the advantage that you can read without having to calculate how often the loop will run.
for i in range(0, 10): # runs 10 times
This is also consistent with other languages which don't have `range` and use oldschool for loops:
for(int i = 0; i < 10; i++); // runs 10 times
for(int = 0; i < array.size(); i++) // runs array.size() times
And having to use (array.size() - 1) every time would of course be silly.
It makes sense for python to go that route since that is what programmers are used to anyway!
+ 3
Also, this is so that range(m, n) doesn't overlap with range(n, k).