What is the use of the range function in Python

In simple terms, range() allows the user to generate a series of numbers within a given range. Depending on how many arguments the user is passing to the function, the user can decide where that series of numbers will begin and end, as well as how big the difference will be between one number and the next. Python range() function takes can be initialized in 3 ways.

  • range (stop) takes one argument.
  • range (start, stop) takes two arguments.
  • range (start, stop, step) takes three arguments.

Python range() function

The Python range() function returns a sequence of numbers, in a given range. The most common use of it is to iterate sequences on a sequence of numbers using Python loops.

Example

In the given example, we are printing the number from 0 to 4.

Python3




for i in range(5):
    print(i, end=" ")
print()


Output:

0 1 2 3 4 

Similar Reads

Syntax of Python range() function

...

What is the use of the range function in Python

Syntax: range(start, stop, step) Parameter : start: [ optional ] start value of the sequence stop: next value after the end value of the sequence step: [ optional ] integer value, denoting the difference between any two numbers in the sequence Return : Returns an object that represents a sequence of numbers...

Python range (stop)

In simple terms, range() allows the user to generate a series of numbers within a given range. Depending on how many arguments the user is passing to the function, the user can decide where that series of numbers will begin and end, as well as how big the difference will be between one number and the next. Python range() function takes can be initialized in 3 ways....

Python range (start, stop)

When the user call range() with one argument, the user will get a series of numbers that starts at 0 and includes every whole number up to, but not including, the number that the user has provided as the stop....

Python range (start, stop, step)

...

Python range() with More Examples

When the user call range() with two arguments, the user gets to decide not only where the series of numbers stops but also where it starts, so the user doesn’t have to start at 0 all the time. Users can use range() to generate a series of numbers from X to Y using range(X, Y)....