Python range() Function: Use, Syntax, and Examples

Python range() function: In this tutorial, we will learn about the range() function in Python with its use, syntax, parameters, returns type, and examples. By Bipin Kumar Last updated : June 24, 2023

Python range() function

The range() function is a library function in Python which returns the sequence of values. It is used where we need to perform a specific action for a limited number of times. In general, if we write range starting from a value i and end up to the value j then we will get a sequence i, i+1 up to j-1.

Syntax

The following is the syntax of range() function:

range(start, end, step)

Parameter(s):

The following are the parameter(s):

  • start: It is an integer from which the sequence has to start that is starting integer of the sequence of the integer.
  • end: It is an integer before which the sequence of integers is to be generated. Generally, if we provide an integer j then it generates sequence up to j-1.
  • step: It is used to determine the difference or increment between each integer in the sequence.

Different ways to call range() function

There are three-way by which we can call the range in the program which is the following,

1. range(end)

We will give only one argument ending in the range() function when we want to start the sequence of the integer with 0. If we give the value of end is j the this is seen as a sequence whose upper limit is j and the lower limit is 0 and step is 0.

2. range(start, end)

When the user decides to generate the sequence starting with a specific integer and also end before a specific integer then they call the range function with the two-argument. When the user gives the value of start then it works like the range function with one argument i.e range(end) because the range() function by default starts the sequence with zero.

3. range(start, end, step)

When we want to generate a sequence where we skip or increment the sequence integer by a value other than 1 that is we want a kind of arithmetic progression which have a common difference greater than 1 then we call the range() function with the three arguments. If we call a range() function with no step then by default it goes for 1.

Let's see an example by which we understand it in a better way.

Python range() Function

#call range() with one argument
print('First sequence:')
for k in range(10):
    print(k,end=' ')
print()

#call range() with two argument
print('Second sequence:')
for k in range(2,10):
    print(k,end=' ')
print()

#call range() with two argument
print('Third sequence:')
for k in range(2,20,1):
    print(k,end=' ')
print()

#call range() with negative step
print('Fourth sequence:')
for k in range(20,1,-2):
    print(k,end=' ')
print()

Output

First sequence:
0 1 2 3 4 5 6 7 8 9 
Second sequence:
2 3 4 5 6 7 8 9 
Third sequence:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 
Fourth sequence:
20 18 16 14 12 10 8 6 4 2 



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.