3 ms·
The 0 indexing is something one can get used to. On the other hand the fact that range(k,n) goes from k,...,n-1 is the most annoying part.
by abdullahkhalids 4y ago
The 0 indexing is something one can get used to. On the other hand the fact that range(k,n) goes from k,...,n-1 is the most annoying part.
- whatshisface 4y agoThat's so that range(10) has 10 items in it.
- abdullahkhalids 4y agoYes. But when I use invoke range(k,n) or list[k:n] I intend to do operations on some specific items, not repeat some operations 10 times. So every time I see `for i in range(10)`, I have to do the mental computation: I am repeating this operation on numbers from 0,...,9. I would rather write `for i in range(9)` and know that I am operating on 0,..,9.
- hgomersall 4y agoFor anyone that spends significant time using these languages, those mental mappings are as natural as the one you seem to think is obvious - then the additional advantages of the approach become apparent. Exclusive upper bounds mean slices work neatly: a[0:10], a[10:20], a[20:30] etc. That's very neatly represented in a loop from zero in steps of ten. That pattern turns up all over the place when accessing arrays. I've spent enough time in MATLAB to have been scarred by that kind of array slicing and off-by-one errors.
- xigoi 4y agoThis has the nice advantage that range(k,n) + range(n,m) == range(k,m).