range
Bjorn Pettersen
BPettersen at NAREX.com
Thu Mar 6 02:58:25 EST 2003
> From: Hilbert [mailto:hilbert at microsoft.com]
>
> Why does range(1,4,1) return [1,2,3]?
> It does not make sense at all.
> It should return [1,2,3,4].
>
> Am I crazy?
Perhaps <wink>.
range(n) returns the exact indices for which a sequence s with len(s) ==
n are valid, so:
for i in range(len(s)):
...
iterates over all elements, while with your version it would have been:
for i in range(len(s)-1):
...
a probably frequent cause for off-by-one errors.
practicality-beats-purity'ly y'rs
-- bjorn
ps: there's a longer rationale based on half-open intervals that you can
probably find on google.
More information about the Python-list
mailing list