inclusive-lower-bound, exclusive-upper-bound (was Re: Range Operation pre-PEP)
Roman Suzi
rnd at onego.ru
Fri May 11 10:55:10 EDT 2001
On Fri, 11 May 2001, Andrew Maizels wrote:
> Aahz Maruch wrote:
> >
> > In article <3AFB0DB9.BDAE54A5 at one.net.au>,
> > Andrew Maizels <andrew at one.net.au> wrote:
> > >
> > >I can see where consistency is important, but why does Python do the
> > >inclusive-lower-bound, exclusive-upper-bound thing?
> >
> > Because it makes loops more likely to work. E.g.:
> >
> > l = [1,4,9,16]
> > for i in range(len(l)):
> > print l[i]
>
> OK, next question: why does Python start indexes at zero? Your example
> would work perfectly well if the range returned [1, 2, 3, 4] and the
> list was indexed starting with 1. Basically, range(4) has to produce a
> list of four items, we just differ on what those items should be.
>
> I'm not just being difficult; I'm trying to design my own language, and
> this is one of the things I have different to Python. If I've missed
> something where the Python way is superior, then I might want to change
> my mind.
>
> The way I have things at the moment, in Pixy (my language), array
> indexes default to start at 1, but can be declared to any range (like
> Pascal). Strings are indexed starting with 1 as well. Is there a good
> reason not to do this?
That is because numbering starts at point 0 and chars are BETWEEN
points - this way inserts are consistent, because you can specify
any insertion range:
A B C D E F
0 1 2 3 4 5 6
- so, you can insert something from 1 to 3 ("BC"),
or from 1 to 1 (""):
>>> a = list("ABCDEF")
>>> a[1:3] = list("QQQ")
>>> print a
['A', 'Q', 'Q', 'Q', 'D', 'E', 'F']
>>> a = list("ABCDEF")
>>> a[1:1] = list("QQQ")
>>> print a
['A', 'Q', 'Q', 'Q', 'B', 'C', 'D', 'E', 'F']
>>>
So, this notation is quite convenient, isn't it?
As an exersize, try to do the same with "convenient"
notation:
A B C D E F
1 2 3 4 5 6
or
A B C D E F
0 1 2 3 4 5
> Andrew.
Sincerely yours, Roman A.Suzi
--
- Petrozavodsk - Karelia - Russia - mailto:rnd at onego.ru -
More information about the Python-list
mailing list