Home » Python

range() function with example in Python

Python range() function: Here, we are going to learn about the range() function in Python with example.
Submitted by Bipin Kumar, on December 10, 2019

Python range() function

The range() is a built-in 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 of range() function:

    range(start, end, step)

It generally takes three arguments which are the following,

  • 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.

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.

Program:

#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

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.