Lada Kugis wrote:
I'm coming from fortran and c background so I'm certainly biased by
them. But if you could explain one thing to me:

in fortran for example:
for i=1,n
goes from 1,2,3,4,...,n

in python for example:
for i in range(1,n)
goes from 1,2,3,4,...,n-1
(that is, it goes from 1 up to, but not including n)

Why is that so ? What were the reasons for that "not including" part ?
It troubles me greatly, and I cannot see it's advantages over the
"standard" "up to and including" n.

Best regards
Lada
--
http://mail.python.org/mailman/listinfo/python-list
This debate has been around for decades, in both mathematics and programming.

Should a loop through n things use indices 1, 2, ..., n
or
   0, 1, ..., n-1 ?

Fortran tends to go with the former (1-based indices) , while modern languages usually go with the latter (0-based indices). (And just for the record, your range(1,n) seems to be trying to coerce Python from 0-based to 1-based.)

The arguments for each are many, but are often centered around the prevalence of the proverbial off-by-one error. Here's a nice explanation of the off-by-one error:
   http://en.wikipedia.org/wiki/Off-by-one_error
Google can provide many more.


My personal view is that 0-based loops/indices is *far* more natural, and the 1-based loops/indices are a *huge* source of off-by-one errors. But I'm sure others will argue over that point.

Gary Herron


--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to