Python Range Function Explained

In Python, the range function is used to generate a sequence of numbers, typically used to control the number of iterations in a loop. There are three forms of the range function.

  1. Generate a sequence of numbers from 0 to stop-1.
  2. Generate a sequence of numbers from start to stop-1.
  3. Generate a sequence of numbers starting from ‘start’, incrementing by ‘step’, up to but not including ‘stop’.

In a loop, the range function can be used to control the number of iterations or generate a sequence of numbers within a specified range. For example:

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

This code will output five numbers from 0 to 4.

bannerAds