Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #43676

Re: Newbie questions on Python

From Neil Cerutti <neilc@norwich.edu>
Newsgroups comp.lang.python
Subject Re: Newbie questions on Python
Date 2013-04-16 15:41 +0000
Organization Norwich University
Message-ID <at59opFg036U1@mid.individual.net> (permalink)
References <a6562e3b-29af-4953-bc8b-49fa634d1be7@googlegroups.com>

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Newbie questions on Python idkfaidkfaidkfa@gmail.com - 2013-04-16 08:20 -0700
  Re: Newbie questions on Python Chris Angelico <rosuav@gmail.com> - 2013-04-17 01:30 +1000
    Re: Newbie questions on Python Walter Hurry <walterhurry@lavabit.com> - 2013-04-16 21:40 +0000
      Re: Newbie questions on Python Chris Angelico <rosuav@gmail.com> - 2013-04-17 07:53 +1000
  Re: Newbie questions on Python Matt Jones <matt.walker.jones@gmail.com> - 2013-04-16 10:33 -0500
  Re: Newbie questions on Python Neil Cerutti <neilc@norwich.edu> - 2013-04-16 15:41 +0000
    Re: Newbie questions on Python Lele Gaifax <lele@metapensiero.it> - 2013-04-16 18:06 +0200
      Re: Newbie questions on Python Neil Cerutti <neilc@norwich.edu> - 2013-04-16 17:16 +0000

csiph-web