Tutorial creates confusion about slices

Neil Cerutti horpner at yahoo.com
Thu Apr 26 15:47:41 CEST 2007


On 2007-04-25, Ant <antroy at gmail.com> wrote:
> On Apr 23, 1:38 pm, Antoon Pardon <apar... at forel.vub.ac.be> wrote:
>> The following is part of the explanation on slices in the
>> tutorial:
>>
>> The best way to remember how slices work is
> ...
>>   +---+---+---+---+---+
>>   | H | e | l | p | A |
>>   +---+---+---+---+---+
>>   0   1   2   3   4   5
>>  -5  -4  -3  -2  -1
>>
>> This is all very well with a simple slice like:
>>
>>   "HelpA"[2:4]    =>     "lp"
>>
>> But it give the wrong idea when using the following extended
>> slice:
>>
>>   "HelpA"[4:2:-1]   =>   "Ap"
>
> I think that the tutorial example is absolutely fine as it is. 

I object only to the word "best". I don't like the above model
because it divorces the indexes that appear in subscripts from
those that appear in slices. I 't find it complicated to think:
a[2:4] is the contiguous slice of elements starting at the gap
between element 1 and 2, and ending at the gap between element 3
and 4. I've always found thinking of [2:4] as a half-open range
much easier.

I suppose the above model could avoid this notational problem if
you say that a[k] means the one element slice a[k:k+1]
(technically true for strings, but false for lists), rather than
ever thinking of item indexes as pointing directly at an item.

So I vote that the word "best" be removed.

-- 
Neil Cerutti
When "yearn" was sung, the performers ounded like they were in a state of
yearning. --Music Lit Essay



More information about the Python-list mailing list