Slice confusion : a[n:p] is a list exclude the last element p

andrew cooke andrew at acooke.org
Mon Apr 28 12:43:42 EDT 2003


Andrew Koenig said:
[...]
> So, for example, to express the integers 4, 5, 6, 7, you can write [4,
> 7], (3, 7], [4, 8), or (3, 8).  Here, the [ ] mean that the range
> *includes* the number next to the bracket, and the ( ) mean that the
> range *excludes* the number next to the parenthesis.  The question,
> then, is which kind of range to prefer.
[...]
> From the foregoing argument, I conclude that it's easiest
> overall to represent *all* ranges asymmetrically: the first
> element of the range and the first element beyond the range.

using [) consistently also gives you nice things like correct addition of
intervals:

[1,4)+[4,7) = [1,7)

isn't there some (fairly old) popular math book by a famousish person
(knuth?  hofstadter?) that develops maths (at least arithmetic) based on
defining sets of points from the "number line"?  i wonder what that says
about this?  (i should admit that i picked up the above from one of
celko's db books).

andrew

-- 
http://www.acooke.org/andrew





More information about the Python-list mailing list