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

Note:: 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])

  • The start specifies where to start the range, e.g. 5. The default value is 0.
  • Where stop is the required parameter.
  • 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 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:

Python range

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:

range for loop

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:

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:

for x in range(20,30):



print ("The current range item: " ,x)

Output:

range start end

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:

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

The following for loop is used with the range function:

for x in range(10,100,10):



print ("The current range item: " ,x)

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 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:

range list

The xrange function in Python

Note: The Python xrange has been renamed to range function from 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 an “xrange object”.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!