# 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,

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

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions