Newbie questions on Python

Neil Cerutti neilc at norwich.edu
Tue Apr 16 11:41:13 EDT 2013


On 2013-04-16, idkfaidkfaidkfa at gmail.com <idkfaidkfaidkfa at gmail.com> 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



More information about the Python-list mailing list