a simple question about the third index in slice

7

700MHz

I cannot quite understand when the third index is a negative
number,like this:
a = '0123456789'
a[1:10:2] I know the index step is 2, so it will collect items from
offset 1, 3, 5, 7, 9
but when a negative number come,like:
a[1::-1] answer '10', and a[1:10:-1] only answer '',
what is the different between the two expression, where does the offset

begin and what is the end offset?
Thanks!
 
F

Fredrik Lundh

someone said:
I cannot quite understand when the third index is a negative
number,like this:
a = '0123456789'
a[1:10:2] I know the index step is 2, so it will collect items from
offset 1, 3, 5, 7, 9
but when a negative number come,like:
a[1::-1] answer '10', and a[1:10:-1] only answer '',
what is the different between the two expression, where does the offset
begin and what is the end offset?

the default values for start and stop depend on the sign of the step.

you can use the slice type directly to see how this works:
(9, -1, -1)
(1, 10, -1)

also see footnote 5 on this page:

http://docs.python.org/lib/typesseq.html

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top