Why are slice indices the way they are in python?

Steve Bergman steve at rueb.com
Wed Nov 29 23:28:35 EST 2006


A couple of off the wall questions.

It seems to me that there is usually a solid *reason* for things in
Python and I'm wondering about the rationale for the way slicing works:

my_string[2:5]

gets you the 3rd through the 3rd through the 5th character of the
string because indexing starts at 0 and you get everything up to, but
not including the second index.

Why?  It doesn't seem intuitive to me.  To me, it makes it harder, not
easier, to work with slices than if indexing started at 1 and  the
above expression got you the 2nd throught the 5th character.

Another thing that I've been puzzling over is the pow() function.

pow(x,y) gives x**y.  Fine.

But pow(x,y,z) gives (x**y) % c

I'm curious to know what the pressing reason for such a feature was.




More information about the Python-list mailing list