How to use Python range and xrange functions with 5 examples

The purpose of range function in Python

The range is a built-in function in Python that represents an immutable sequence of numbers.  Generally, Python range is used in the for loop to iterate a block of code for the 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 end 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 the step rather than 1.

All these are shown in the examples in the section after syntax of using range in Python.

PS: To learn about xrange function and difference between range and xrange, go to the last part of this tutorial.

Syntax of range function

The general syntax of creating a range is:

range([start], stop, [step])

  • Where, stop is the required parameter.
  • The start specifies where to start the range, e.g. 5. The default value is 0.
  • 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 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 step and stop are not specified. The print function is used to display the range:

Python range

See online demo and code

This is how the range is created and displayed:

 

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.

range for loop

See online demo and code

The following code is used in the example:

 

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 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 the for loop, as shown below:

range start end

See online demo and code

This is how the range is created:

range(20,30)

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:

 

A demo of specifying start, end, and step numbers in range function

This time, a range is given with all three numbers in a for loop:

range(10,100,10)

This specifies to start range from 10 and end at 100 with a step of 10. See the code and output in the demo page by clicking the links below:

range start end step

See online demo and code

The following for loop is used with the range function:

 

This yielded the output as:

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 negative range

The following example shows how you may create a list with negative numbers by using the range function:

range list

See online demo and code

This code is executed:

 

The xrange function in Python

Note: The Python xrange has been renamed to range function from the version 3.x of Python. While the range function in Python 2.x is removed.

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 a “xrange object”.