well, from the help:
>>> help(range)
range(...)
range([start,] stop[, step]) -> list of integers
Return a list containing an arithmetic progression of integers.
range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
When step is given, it specifies the increment (or decrement).
For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!
These are exactly the valid indices for a list of 4 elements.
so the last increment is not stop, but the last step before stop.
- in countMe shouldn't the code go up till 18 ;
- why is the last number printed in countMe 15, and not 18 ;
- why is that in the second function oddsOut the function only founts till 7 for j and not 8 even though j is 8 ;
- why is the last number printed in oddsOut 14.
more generally speaking the answer to those questions is that in most of the languages, a range is defined as [start:stop[, i.e. the last value of the range is never included, and the indexes start always at 0. The mess being that in a few languages and when working on algorithmics, ranges start at 1 and are inclusive with the last value.
In the end, if you want to include the last value you can do:
def closed_range(start, stop, step=1):
return range(start, stop+1, step)
or in your example:
>>> def countMe(num):
>>> for i in range(0, num+1, 3):
>>> print (i)
>>>
>>> countMe(18)
0
3
6
9
12
15
18
>>>
stopparameter (the second one) of range is exclusive not inclusive.rangefunction, slices, etc. This is explained in the tutorial chapter on Strings, the first place slices are introduced, and should show up similarly early in any other tutorials/texts/etc.numtonum + 3random.randint.numtonum+1.