2
def countMe(num):
    for i in range(0, num, 3):
        print (i)

countMe(18)

def oddsOut(num1, num2):

    for i in range(num1):
        for j in range(num2):
            print(i*j)

oddsOut(3, 8)

I don't understand how the range function works:

  • 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 counts till 7 for j and not 8 even though j is 8 ;
  • why is the last number printed in oddsOut 14.
11
  • 8
    The stop parameter (the second one) of range is exclusive not inclusive. Commented Jan 24, 2014 at 20:03
  • 3
    Almost all ranges things in Python are "half-open", meaning that the start is inclusive and the stop is exclusive—the range function, 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. Commented Jan 24, 2014 at 20:08
  • If you want it to include 18, maybe just change num to num + 3 Commented Jan 24, 2014 at 20:09
  • The only thing in the stdlibs I know, where the end point is included is random.randint. Commented Jan 24, 2014 at 20:11
  • @user3193087 actually, no. You need to change num to num+1. Commented Jan 24, 2014 at 20:13

3 Answers 3

4

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
>>> 
Sign up to request clarification or add additional context in comments.

1 Comment

stop-step? So the last number yielded by range (0, 100, 7) would be 93? (it is 98 actually)
2

The stop parameter in a range does not include that number for example

for i in range(0,5):
    print i

would print 0-4 but not 5.

Comments

0

Ranges in Python do not include the ending value. This is consistent with slices.

If you need a way to remember this, consider that range(10) has 10 elements - the numbers 0 through 9.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.