You know, I've been using code like that for a while now and yet never knew why it had to be iX+2.
Hey, a chance to share my expertise!
*dons Python teacher's hat*
In Python, whenever you have to specify a range of values (like with the range() function, or taking part of a list as in myvalues[1:5]), it uses what's called
half-open intervals. If you've forgotten that bit of your high school math education, here's a refresher:
Closed intervals: both the boundary numbers are included in the interval. The range 1-5 as a closed interval would consist of the numbers 1, 2, 3, 4 and 5.
Open intervals: neither of the boundary numbers are included in the interval. The range 1-5 as a closed interval would consist
only of the numbers 2, 3 and 4.
Half-open intervals: one of the boundary numbers, but not the other one, is included. In Python, the lower number is included, but not the upper number. So the range 1-5 as a half-open interval (closed on the bottom) would consist of the numbers 1, 2, 3 and 4 (but not 5).
Closed intervals are more intuitive: when you say "1 through 5", you usually mean that 1 and 5 should be included. But closed intervals have some counter-intuitive behaviors, too: to count the number of items in the list, you have to do (max - min + 1). By contrast, with a half-open interval, the number of items is simply (max - min).
The other advantage of a half-open interval is that if you're trying to split it into multiple pieces, the pieces stack together neatly. For example, say you have a list that needs to be split at points a, b, c and d. (And let's say you've also defined "start" and "end" to be the start and end of the segment that needs to be split). With half-open intervals, this is what it would look like:
part1 = list[start:a]
part2 = list[a:b]
part3 = list[b:c]
part4 = list[c:d]
part5 = list[d:end]
With closed intervals, you'd need to add +1's or -1's in the right places, and there's lots of room for one-off errors; with half-open intervals, splitting a list into segments is elegant and error-free.
So that's why Python uses half-open intervals: you have to remember a +1 when specifying a range, but you can omit the +1 or -1 when splitting a list or finding its length. It's a trade-off.