array slices in python

Terry Reedy tjreedy at udel.edu
Mon May 12 21:01:01 EDT 2003


<beliavsky at aol.com> wrote in message
news:3064b51d.0305121619.48632d35 at posting.google.com...
> In Python, "a[1:3]" refers to elements a[1] and a[2].
> In Fortran 95, "a(1:3)" refers to elements a(1), a(2), and a(3),
which
> seems more natural. Matlab works the same way.
>
> What is the rationale for the Python convention for array slices?

Think of slice positions as being cursor positions *between* sequence
items, like the vertical bar cursor of gui editors (as opposed to the
block on top of char cursors of older fixed pitch display editors).
Counting the end positions, there are n+1 slice positions for n
items -- ranging from 0 to n.  If that doesn't do it for you, follow
the ref in Fernando's response.

Terry J. Reedy






More information about the Python-list mailing list