Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Neil Cerutti Newsgroups: comp.lang.python Subject: Re: Newbie questions on Python Date: 16 Apr 2013 15:41:13 GMT Organization: Norwich University Lines: 26 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: individual.net C9FvUVVYVcPDfkS52eVO0grvgRJjjq0wxVQDgu2QIJaBHdjT2e Cancel-Lock: sha1:r+03zAllNW3aDElphoBXKv4uW38= User-Agent: slrn/0.9.9p1/mm/ao (Win32) Xref: csiph.com comp.lang.python:43676 On 2013-04-16, idkfaidkfaidkfa@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