Skip to content

5.6 Counting using range() function

  • The range() function allows counting in for loops as well.
  • range() generates a sequence of integers between a starting integer that is included in the range, an ending integer that is not included in the range, and an integer step value.
  • The sequence is generated by starting at the start integer and incrementing by the step value until the ending integer is reached or surpassed.
  • The range() function can take up to three integer arguments:
    • range(Y) generates a sequence of all non-negative integers less than Y. Ex: range(3) creates the sequence 0, 1, 2.
    • range(X, Y) generates a sequence of all integers >= X and < Y. Ex: range(-7, -3) creates the sequence -7, -6, -5, -4.
    • range(X, Y, Z), where Z is positive, generates a sequence of all integers >= X and < Y, incrementing by Z. Ex: range(0, 50, 10) creates the sequence 0, 10, 20, 30, 40.
    • range(X, Y, Z), where Z is negative, generates a sequence of all integers <= X and > Y, incrementing by Z. Ex: range(3, -1, -1) creates the sequence 3, 2, 1, 0.

5%206%20Counting%20using%20range()%20function%20e309ea159fe244ed8df07edec9edf226/Untitled.png