I do this mistake again and again, range(3)[1:2] does not return [1,2] but [1]. Could someone explain the logic twist here? Why are m and n not referring to x-th element with the same logic?
I do this mistake again and again, range(3)[1:2] does not return [1,2] but [1]
Share
Slices and ranges are a pair of numbers: the first element to include, and then the first element not to include. By doing this, you get a few nice benefits. First, the length of the slice is end-start. Second, the slices [x:y] and [y:z] will fit nicely together without duplicating y.
A recent thread challenging this design on Python-Ideas: http://mail.python.org/pipermail/python-ideas/2010-October/008187.html
Edsger Dykstra wrote about this in his inimitable style: http://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF , which also covers why the first element is [0].