[Tutor] Slicing Tuples

John Russell thor at othala.us
Sat Dec 11 17:25:49 CET 2010


Last night I started working through a book (Beginning Python: Using Python
2.6 and Python 3.1)  I bought to learn Python, and there is an example in it
that doesn't make sense to me.

There is an example on slicing sequences that goes like this:

slice_me=("The", "next", "time", "we","meet","the","drinks","are","on","me")
sliced_tuple=slice_me[5:9]
print(sliced_tuple)

which, results in

('drinks', 'are', 'on', 'me')

there is an example a little further below that shows the same concept, only
applying it to a string, and, the result is  four characters long, starting
at the 6th position.

Now, I understand why they start where they do, because the counting starts
at 0, so 5 is the 6th element. What I don't understand is why they end where
they do. By my count, 5 to 9 would be 5 elements, not 4. With the tuple, I
thought the result was because there wasn't enough elements left, but when I
changed 5:9 to 5:8, it returned one less result.

So, my question is this, and I realize that this is *very* basic - what is
going on with the last element? Why is it returning one less than I think it
logically should. Am I missing something here? There is not much of an
explanation in the book, but I would really like to understand what's going
on here.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101211/478ea596/attachment.html>


More information about the Tutor mailing list