Why are slice indices the way they are in python?
Terry Reedy
tjreedy at udel.edu
Wed Nov 29 23:47:25 EST 2006
"Steve Bergman" <steve at rueb.com> wrote in message
news:1164860915.349821.309920 at 14g2000cws.googlegroups.com...
>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
Yes, see below.
> 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?
len(s[2:5])== 5-2
s[2:5] + s[5:7] == s[2:7]
> 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
you obvious meant (x**y) % z
>
> I'm curious to know what the pressing reason for such a feature was.
Such exponential remainders are used in cryptography work, for instance,
with fairly large values, and can be efficiently computed, especially in C,
*without* computing a humongous x**y intermediate value.
Since this is an int function, it does not really belong in the floating
point math module.
Terry Jan Reedy
More information about the Python-list
mailing list