Tuesday 15 June 2010

Why does range in python "stop short"? -


OK, teach children dragon. We have just written our first short program:

  b = 0 for a range (1, 10) b = b + 1 print a, b   

9, 9 stops They asked, "why is it" and I can not say that I know the answer.

My code always contains the files, and one row for my "in the reader" line is not low, so I do not really know. In mathematical notation, this behavior would be [1,10] . Technically, (1,10) will be 2,3,4,5,6,7,8,9 , and in fact me [1, 10] should .

Excerpt from this works only from Python's category :

This is a versatile task for creating lists of arithmetic progression. Logic should be simple integer, this is most often used for loop. If the phase logic is left out, then it is a mistake for 1. If the start argument is left out, then it misses 0. The complete form gives the list of plain integers [start, start + step, start + 2 * steps, ...]. If the phase is positive, the last element is the lower step than the biggest start + i * stop; If the step is negative, then the last element is the smallest start + step above i + stop. Phase must not be zero (or otherwise ValueError has been raised)

No comments:

Post a Comment