Python + strange == cool

Discussion in 'Python' started by Maboroshi, Sep 29, 2004.

  1. Maboroshi

    Maboroshi Guest

    All fine and good <below>

    >>> x = ["list1", "list2", "list3", 4, 5, 6, 7]
    >>> x[1:-1]

    ['list2', 'list3', 4, 5, 6] # All good here

    this struck me as weird I had an idea to mess around with lists <below>

    >>> x[1:+1]

    []

    now when I do this
    >>> x[1:+2]

    ['list2']

    Does this puzzle anyone else

    of course I am a total novice so I have no idea what I am doing here if
    anyone has any ideas please reply
     
    Maboroshi, Sep 29, 2004
    #1
    1. Advertising

  2. Maboroshi

    Phil Frost Guest

    The second number in the slice is the index of the last element to be
    included, exclusive. It is not an offset from the first index. Adding
    the "+" does nothing; that just means "positive". As the laws of
    mathematics dictate, "+n = n"; the unary + is just for symmetry with the
    unary '-'.

    On Tue, Sep 28, 2004 at 04:21:12PM -0700, Maboroshi wrote:
    > All fine and good <below>
    >
    > >>> x = ["list1", "list2", "list3", 4, 5, 6, 7]
    > >>> x[1:-1]

    > ['list2', 'list3', 4, 5, 6] # All good here
    >
    > this struck me as weird I had an idea to mess around with lists <below>
    >
    > >>> x[1:+1]

    > []
    >
    > now when I do this
    > >>> x[1:+2]

    > ['list2']
    >
    > Does this puzzle anyone else
    >
    > of course I am a total novice so I have no idea what I am doing here if
    > anyone has any ideas please reply
     
    Phil Frost, Sep 29, 2004
    #2
    1. Advertising

  3. Maboroshi

    Maboroshi Guest

    Ok that makes sense

    Cheers








    Phil Frost wrote:
    > The second number in the slice is the index of the last element to be
    > included, exclusive. It is not an offset from the first index. Adding
    > the "+" does nothing; that just means "positive". As the laws of
    > mathematics dictate, "+n = n"; the unary + is just for symmetry with the
    > unary '-'.
    >
    > On Tue, Sep 28, 2004 at 04:21:12PM -0700, Maboroshi wrote:
    >
    >>All fine and good <below>
    >>
    >>>>>x = ["list1", "list2", "list3", 4, 5, 6, 7]
    >>>>>x[1:-1]

    >>
    >>['list2', 'list3', 4, 5, 6] # All good here
    >>
    >>this struck me as weird I had an idea to mess around with lists <below>
    >>
    >>>>>x[1:+1]

    >>
    >>[]
    >>
    >>now when I do this
    >>
    >>>>>x[1:+2]

    >>
    >>['list2']
    >>
    >>Does this puzzle anyone else
    >>
    >>of course I am a total novice so I have no idea what I am doing here if
    >>anyone has any ideas please reply
     
    Maboroshi, Sep 29, 2004
    #3
  4. Maboroshi

    Dave Brueck Guest

    Maboroshi wrote:
    > All fine and good <below>
    >
    > >>> x = ["list1", "list2", "list3", 4, 5, 6, 7]
    > >>> x[1:-1]

    > ['list2', 'list3', 4, 5, 6] # All good here
    >
    > this struck me as weird I had an idea to mess around with lists <below>
    >
    > >>> x[1:+1]

    > []
    >
    > now when I do this
    > >>> x[1:+2]

    > ['list2']
    >
    > Does this puzzle anyone else
    >
    > of course I am a total novice so I have no idea what I am doing here if
    > anyone has any ideas please reply


    Look at section 3.1.2 of the Python tutorial (look at the whole tutorial if you
    haven't already ;-) ). The part explaining the above starts like this:

    "The best way to remember how slices work is to think of the indices as pointing
    between characters..."

    (note that 3.1.2 is talking about strings, but lists, tuples, and strings are
    all sequences and share many properties. Also, working with strings might make
    it easier to grasp what's going on, and once you've got those down then applying
    the principles to lists and tuples will be a breeze)

    -Dave
     
    Dave Brueck, Sep 29, 2004
    #4
  5. On Tue, 28 Sep 2004 19:30:35 -0400, Phil Frost <>
    wrote:

    >the unary + is just for symmetry with the
    >unary '-'.


    I was bitten in my first python program by writing

    ++x

    :)

    Andrea
     
    Andrea Griffini, Sep 29, 2004
    #5
    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. Posadas, Dennis
    Replies:
    2
    Views:
    308
    Tony Clarke
    Dec 2, 2003
  2. Replies:
    0
    Views:
    524
  3. Replies:
    6
    Views:
    6,673
    John Bokma
    Jul 17, 2006
  4. Tony Arcieri
    Replies:
    14
    Views:
    466
    Tony Arcieri
    Dec 9, 2010
  5. Tony Arcieri
    Replies:
    0
    Views:
    415
    Tony Arcieri
    Dec 14, 2010
Loading...

Share This Page