Python For loop and range function

Issue

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.

Solution

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.

Answered By – aidnani8

Answer Checked By – Clifford M. (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.