The purpose of range function in Python
Generally, Python range is used in the for loop to iterate a block of code for a given number of times.
For example, you may create a range of five numbers and use with for loop to iterate through the given code five times.
- By default, the range starts from 0 and steps at 1. So in a case of a range of 5, it will start from 0 and ends at 4.
- You may also specify the start and end of numbers, e.g. starting from 5 and ending at 9.
- You may also specify a step other than 1.
All these are shown in the examples in the section after the syntax of using a range in Python.
Syntax of range function
The general syntax of creating a range is:
Parameters | Description |
start | The start specifies where to start the range, e.g. 5. The default value is 0. |
stop | The stop is the required parameter. |
step | The step specifies the step between each number. The default is 1. |
See the following examples of creating a simple range and using different parameters. I will also show, how you may use the range Python function with a for loop.
An example of creating a simple range
In this example, a range of 5 numbers is created. Only the stop parameter is given while the start and stop are not specified. The print function is used to display the range.
This is how the range is created and displayed:
tst_range = range(10) print (tst_range)
This Outputs:
Using a for loop to display range numbers
This time, a for loop is used where a range of ten numbers is given. In each iteration, the range numbers are displayed.
The following code is used in the example:
for x in range(10): print (x)
The above code output:
1
2
3
4
5
6
7
8
9
The output displayed the numbers from 0 to 9, with the step of 1, as such, no start and step parameters were given.
An example of range with a start parameter
In this demo, the start number is given in the Python range function. A range of ten numbers that starts from 20 is created and displayed by using Python for loop, as shown below:
This is how the range is created:
That means, start a range from 20 and end at 29. As such, no step is given, so output is with the gap of one:
for x in range(20,30): print ("The current range item: " ,x)
Output:
A demo of specifying start, end, and step numbers in the range function
This time, a range is given with all three numbers in a for loop:
This specifies to start range from 10 and end at 100 with a step of 10. See the code and output below.
The following for loop is used with the range function:
for x in range(10,100,10): print ("The current range item: " ,x)
The output:
The current range item: 10
The current range item: 20
The current range item: 30
The current range item: 40
The current range item: 50
The current range item: 60
The current range item: 70
The current range item: 80
The current range item: 90
An example of a negative range
The following example shows how you may create a list with negative numbers by using the range function:
This code is executed:
list(range(0, -10, -1))
Output:
The xrange function in Python
If you have used xrange in previous versions of Python (2.x) then either rename it manually or alternatively, you may use this automated program from Python to make it compatible with 3.x version.
The difference
The only difference between the two functions was (in the previous version), the range function used to return a list while xrange function returned an “xrange object”.