Why are slice indices the way they are in python?

Discussion in 'Python' started by Steve Bergman, Nov 30, 2006.

  1. A couple of off the wall questions.

    It seems to me that there is usually a solid *reason* for things in
    Python and I'm wondering about the rationale for the way slicing works:

    my_string[2:5]

    gets you the 3rd through the 3rd through the 5th character of the
    string because indexing starts at 0 and you get everything up to, but
    not including the second index.

    Why? It doesn't seem intuitive to me. To me, it makes it harder, not
    easier, to work with slices than if indexing started at 1 and the
    above expression got you the 2nd throught the 5th character.

    Another thing that I've been puzzling over is the pow() function.

    pow(x,y) gives x**y. Fine.

    But pow(x,y,z) gives (x**y) % c

    I'm curious to know what the pressing reason for such a feature was.
    Steve Bergman, Nov 30, 2006
    #1
    1. Advertising

  2. Steve Bergman

    Terry Reedy Guest

    "Steve Bergman" <> wrote in message
    news:...
    >A couple of off the wall questions.
    >
    > It seems to me that there is usually a solid *reason* for things in
    > Python and I'm wondering about the rationale for the way slicing works


    Yes, see below.

    > my_string[2:5]
    >
    > gets you the 3rd through the 3rd through the 5th character of the
    > string because indexing starts at 0 and you get everything up to, but
    > not including the second index.
    >
    > Why?


    len(s[2:5])== 5-2
    s[2:5] + s[5:7] == s[2:7]


    > Another thing that I've been puzzling over is the pow() function.
    >
    > pow(x,y) gives x**y. Fine.
    >
    > But pow(x,y,z) gives (x**y) % c


    you obvious meant (x**y) % z

    >
    > I'm curious to know what the pressing reason for such a feature was.


    Such exponential remainders are used in cryptography work, for instance,
    with fairly large values, and can be efficiently computed, especially in C,
    *without* computing a humongous x**y intermediate value.

    Since this is an int function, it does not really belong in the floating
    point math module.

    Terry Jan Reedy
    Terry Reedy, Nov 30, 2006
    #2
    1. Advertising

  3. On 29 Nov 2006 20:28:35 -0800, "Steve Bergman" <>
    declaimed the following in comp.lang.python:

    >
    > Why? It doesn't seem intuitive to me. To me, it makes it harder, not
    > easier, to work with slices than if indexing started at 1 and the
    > above expression got you the 2nd throught the 5th character.
    >

    Indices for slicing can be looked at as identifying the "slot"
    BEFORE elements..

    1 2 3 4 5 6 7 8
    A S t r i n g
    | | | | | | | |
    0 1 2 3 4 5 6 7
    ^ ^ ^
    2:5 take after 2, and to before 5

    >
    > But pow(x,y,z) gives (x**y) % c
    >
    > I'm curious to know what the pressing reason for such a feature was.


    Probably cryptography <G>
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
    Dennis Lee Bieber, Nov 30, 2006
    #3
  4. Steve Bergman

    Ant Guest

    Steve Bergman wrote:

    ....
    > Why? It doesn't seem intuitive to me. To me, it makes it harder, not
    > easier, to work with slices than if indexing started at 1 and the
    > above expression got you the 2nd throught the 5th character.


    Dijkstra has an article about this:
    http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html

    In practice it probably wouldn't make a great deal of difference. Here
    at my current job, we have a situation where a bunch of Progress 4GL
    programmers have been taught Java and let loose on the code base. we
    now have a situation where there are a plethora of methods written
    designed to be 1-based, and you have to trawl through the code to work
    out if the method was written by a Java programmer or a 4GL programmer
    to save on array index exceptions...

    The infuriating thing is that if you are iterating over an array in
    Java, and have to use one of the functions based on an array index, you
    have to use function(i + 1). Drilling back through the code, and
    invariably you have the function written:

    def function(i):
    return array[i-1]

    !!! (at least you would if the Java was written in Python by 4GL
    programmers, but you get the idea ;-) )
    Ant, Nov 30, 2006
    #4
  5. Steve Bergman

    Guest


    > Why? It doesn't seem intuitive to me. To me, it makes it harder, not
    > easier, to work with slices than if indexing started at 1 and the
    > above expression got you the 2nd throught the 5th character.


    Zero-based indices and excluding last index often works nicer, it is
    not the rule, but all my experience says that.

    a[i:i] is very handy to have in many algorithms, so you don't have to
    exclude this case explicitly.
    Index starting at 1 or including N often adds +-1 expressions to the
    code. I think that http://en.wikipedia.org/wiki/Modular_arithmetic
    makes it more intuitive.

    And if talking about dates, then I suggest NEVER use 2006-12-31
    23:59:59 in data, always 2007-01-01 00:00:00 instead. If customer wants
    to see former on the screen just substruct 1 millisecond from the data
    and round it to what is needed. It makes all arithmetics, finding
    period collapses, time differences, etc much more painless. You
    sometimes don't even know if your database supports seconds,
    milliseconds, microseconds and what your operating system and
    programming language support. So if you are using inclusive end date
    you may find yourself in trouble very soon.

    Again, its not intuitive for ordinary people, but math people and
    programmers often find 0-based and exlusive ends are nicer to work
    with, at least me :)

    > Another thing that I've been puzzling over is the pow() function.
    >
    > pow(x,y) gives x**y. Fine.
    >
    > But pow(x,y,z) gives (x**y) % c
    >
    > I'm curious to know what the pressing reason for such a feature was.

    This makes cryptography code 100 times faster.

    Oleg
    , Nov 30, 2006
    #5
  6. On Thu, 2006-11-30 at 08:58 -0800, wrote:
    > And if talking about dates, then I suggest NEVER use 2006-12-31
    > 23:59:59 in data, always 2007-01-01 00:00:00 instead.


    Forgive me for my ignorance, but isn't "2006-12-31 23:59:59" actually
    one entire second earlier than "2007-01-01 00:00:00"? Hence they are
    two different dates are they not?

    Michael
    Michael Torrie, Dec 1, 2006
    #6
  7. Michael Torrie wrote:

    > Forgive me for my ignorance, but isn't "2006-12-31 23:59:59" actually
    > one entire second earlier than "2007-01-01 00:00:00"? Hence they are
    > two different dates are they not?


    given the context, maybe the poster meant that he's storing date
    *ranges* as [start, stop) (i.e. including start but not including
    stop).

    </F>
    Fredrik Lundh, Dec 1, 2006
    #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. Asfand Yar Qazi
    Replies:
    4
    Views:
    386
    Asfand Yar Qazi
    Nov 12, 2004
  2. Martin M.
    Replies:
    4
    Views:
    335
    Simon Brunning
    Dec 15, 2005
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,762
    Smokey Grindel
    Dec 2, 2006
  4. Replies:
    2
    Views:
    399
  5. Billy Mays

    Better way to iterate over indices?

    Billy Mays, Jun 21, 2011, in forum: Python
    Replies:
    4
    Views:
    182
    Ethan Furman
    Jun 21, 2011
Loading...

Share This Page