Arithmetic sequences in Python

Gregory Petrosyan gregory.petrosyan at gmail.com
Mon Jan 16 17:22:56 EST 2006


Steven D'Aprano wrote:
> Python indexing deliberately goes to one-past-the-end counting for a
> reason: it helps prevent off-by-one signpost errors. This syntax goes
> against that decision, and adds one more thing to memorise about Python:
> the end index is not included in the list, except for arithmetic
> sequences, where it is, sometimes but not necessarily. In [5,6,10] the end
> index 10 is included; in [5,7,10] it isn't.

1) both in [5,6,10] and [5,7,10] 10 is included ;-)
    And as for [5,6 .. 10] and [5,7 .. 10]... First one should look
like [5 .. 10] I think. But you are right:
    it looks like a problem that in one case 10 is included and in
other not. I should think about it.

2) I think there's nothing to memorise: in mathematics [1,2 .. 10]
includes 10, almost everybody knows
    it.
 The thing that newcomer should memorise is (IMHO) _not_so_obvious_
behaviour of 'range'. You
    may laugh at me, if you want. But look at Cormen's pseudocode: "for
j=1 to n" includes 'n'. (And
    indexing starts from 1, not from 0, but that's another story).
Also, for non-programmer including the
    borders is obvious, not otherwise.




More information about the Python-list mailing list