On 2013-04-16, [email protected] <[email protected]> wrote:
> Hi all,
> i'm programming in python for the first time (usually i use C as programming
> language). I don't understand these results:
>
>>>> a=[1,2,3,4,5]
>>>> a[:-1]
> [1, 2, 3, 4]
>>>> a[::-1]
> [5, 4, 3, 2, 1]
>>>> a[2::-1]
> [3, 2, 1]
The third item is the "step". The default value is 1. If you
provide a negative step, your slice will be in reverse. So you
are getting item 2 through 0 in reverse order in your result
slice.
Imagine something like the following for loop taking place
somewhere:
for (int i = 2; i <= 0; --i) {
fprintf(a[i]);
}
--
Neil Cerutti
--
http://mail.python.org/mailman/listinfo/python-list