Newbie questions on Python

Discussion in 'Python' started by idkfaidkfaidkfa@gmail.com, Apr 16, 2013.

  1. Guest

    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]

    what does a[2::-1] means?
    Thanks
    , Apr 16, 2013
    #1
    1. Advertising

  2. On Wed, Apr 17, 2013 at 1:20 AM, <> 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]
    >
    > what does a[2::-1] means?


    That's taking a slice. This page has something to say on the subject:

    http://docs.python.org/3.3/tutorial/introduction.html

    By the way, regarding your email address: there are no cheat codes in
    Python... either that, or Python *is* a cheat code. :)

    ChrisA
    [[ VERY HAPPY CODING ADDED ]]
    Chris Angelico, Apr 16, 2013
    #2
    1. Advertising

  3. Matt Jones Guest

    When slicing: l[start:end:step]

    In your example of "a[2::-1]" you are reversing the list by using a step of
    -1, then you are slicing at index 2 (third element).

    *Matt Jones*


    On Tue, Apr 16, 2013 at 10:30 AM, Chris Angelico <> wrote:

    > On Wed, Apr 17, 2013 at 1:20 AM, <> 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]
    > >
    > > what does a[2::-1] means?

    >
    > That's taking a slice. This page has something to say on the subject:
    >
    > http://docs.python.org/3.3/tutorial/introduction.html
    >
    > By the way, regarding your email address: there are no cheat codes in
    > Python... either that, or Python *is* a cheat code. :)
    >
    > ChrisA
    > [[ VERY HAPPY CODING ADDED ]]
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    Matt Jones, Apr 16, 2013
    #3
  4. Neil Cerutti Guest

    On 2013-04-16, <> 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);
    }

    --
    Neil Cerutti
    Neil Cerutti, Apr 16, 2013
    #4
  5. Lele Gaifax Guest

    Neil Cerutti <> writes:

    > Imagine something like the following for loop taking place
    > somewhere:
    >
    > for (int i = 2; i <= 0; --i) {
    > fprintf(a);
    > }


    Neil most probably meant

    for (int i = 2; i >= 0; --i) {
    fprintf(a);
    }

    where "fprintf" is actually a fictitious "do_something" function.

    ciao, lele.
    --
    nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
    real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
    | -- Fortunato Depero, 1929.
    Lele Gaifax, Apr 16, 2013
    #5
  6. Neil Cerutti Guest

    On 2013-04-16, Lele Gaifax <> wrote:
    > Neil Cerutti <> writes:
    >
    >> Imagine something like the following for loop taking place
    >> somewhere:
    >>
    >> for (int i = 2; i <= 0; --i) {
    >> fprintf(a);
    >> }

    >
    > Neil most probably meant
    >
    > for (int i = 2; i >= 0; --i) {
    > fprintf(a);
    > }
    >
    > where "fprintf" is actually a fictitious "do_something" function.
    >
    > ciao, lele.


    Thanks for the correction.

    --
    Neil Cerutti
    Neil Cerutti, Apr 16, 2013
    #6
  7. On Wed, Apr 17, 2013 at 7:40 AM, Walter Hurry <> wrote:
    > On Wed, 17 Apr 2013 01:30:03 +1000, Chris Angelico wrote:
    >
    >> By the way, regarding your email address: there are no cheat codes in
    >> Python

    >
    > ROFLMAO. Incidentally, my son used to use IDDQD rather than IDKFA.
    >
    > I of course spurned all such, since I preferred to do it the hard way.
    > Thus I was Doomed.


    I'd sometimes use IDDQD/IDFA (no K) and then see how quickly I could
    blitz the levels, with proper navigation. There's something inherently
    fun about blasting through everything with the rocket launcher.

    Mind you, I was really cheap, so I never bought Doom... and was just
    playing through the demo over and over.

    ChrisA
    Chris Angelico, Apr 16, 2013
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luther Barnum
    Replies:
    26
    Views:
    578
    Harry George
    Oct 27, 2003
  2. Ondrej Krajicek

    Python/COM/ADSI - Newbie questions

    Ondrej Krajicek, Jul 27, 2004, in forum: Python
    Replies:
    0
    Views:
    850
    Ondrej Krajicek
    Jul 27, 2004
  3. walterbyrd

    easy questions from python newbie

    walterbyrd, Jul 24, 2006, in forum: Python
    Replies:
    6
    Views:
    425
  4. Ali Syed
    Replies:
    3
    Views:
    536
    Mark McIntyre
    Oct 13, 2004
  5. JohnE

    newbie with newbie questions

    JohnE, Aug 17, 2009, in forum: ASP .Net
    Replies:
    3
    Views:
    478
    Gregory A. Beamer
    Aug 17, 2009
Loading...

Share This Page