What is the difference between Range() and Xrange() in Python?

The Range() or Xrange() functions in Python can be used in many parts of the program. They help to reduce the size of code and improve its readability.

First, let’s discuss the Range() method and its syntax. We will then elaborate on the Xrange() method.

Range () vs Xrange ()

Attributes

Range()

Xrange()

Parameters

Numerical Values

Numerical Values

Return type

It gives a list of integers

It gives a generator object

Memory Utilization

It gives a list of elements and takes more memory

It takes less memory less range ()

Efficiency

Its execution time is slow

Its execution time is fast

Range() method

This method accepts only positive and negative integers. It does not support floating point or string/character values.

Signatures

range(stop_value)
range(start_value, stop_value)
range(start_value, stop_value, jump_size)

Parameters

The Range() method accepts a single argument. It can accept, at maximum, up to three values as an argument.

  • stop_value: Condition to stop the execution.

Keep in mind that the returned list does not include stop_value.

  • start_value: Defines the starting value where execution will begin. Default = 0.
  • jump_size: Defines the jump size it will take when execution starts. Default = 1.

Return value

Range object: The function returns a list or sequence of numbers.

Example code

In the following example, the Range() method will generate 10 numbers from 0,1...9. The code snippet below prints these n=10 values, separated by commas, on Line 4 as an argument to the print() function.

# First Variant of Range() function
# it only accepts single value and return a list
for n in range (10):
print("Iteration#", n)
  • range(stop_value)

In the next example, we print the first 10 numbers, starting from 0 to 9.

In the function, we give the range() function for the same (with a comma at the end) as parameters to print out. The function will also check what type it returns n = range (10) – > type (n).

# First Variant of Range() function
# it only accepts single value and return a list
for n in range (10):
print (n)
print('\n')
  • range(start_value, stop_value)

In the next example, we print the numbers from 22 to 33. This time, however, we use the reversed() method of Python.

We have provided the value in the range() function for these numbers (with a comma at the end) as parameters to print out. The starting value is 22 and the stopping value is 33.

# starting value = 22
# Stopping value = 33
for n in reversed (range (22,33)):
print (n, end= ',')
  • range(start_value, stop_value, jump_size)

In the next example, we print the values from 0 to 10 in reverse order. We give the value in the range function in the form of start, stop, and end.

# reverse prirting with Step = -1
# Will generate 10 numbers: 0 to 10
for n in range (10, 0,-1):
print (n, end= ',')

Xrange() method

The Xrange() function is similar to rangesee line 2 from the example below. It returns an object of type xrange.

The syntax of Xrange() is the same as Range(), which means we still have to specify start, stop, and step values.

The Xrange method was deprecated in Python 3 but is available for older versions, such as Python 2.x. If you execute a program using Xrange in Python 3, it will return an error because Range() is used from this version onwards.

Signature

Xrange(stop_value)//only accepts stopping condition
Xrange(start_value, stop_value)//accepts starting and stoping value
Xrange(start_value,stop_value,jump_size)//takes starting, stoping, step size

Parameters

The Xrange() method accepts a single argument. It can accept, at maximum, up to three values as an argument.

  • stop_value: Condition to stop the execution.
  • start_value: Defines starting value where we will begin.
  • jump_size: Defines jump size we will take when execution begins.

Return value

Xrange object: It returns an iterable object, which can be iterated multiple times to extract values.

Xrange() returns an iterable object, not an iterator. These are different concepts. You can get the behavior of an iterable by using: numbers = iter(xrange(10)).

Example code

This code snippet shows how Xrange() works. On lines 3 and 7, we use its two different variants.

# Using range() method
print("range(10,30,2) called")
for n in range(10,30,2):
print(n)
# showing Xrange() results on console
# Xrange() method taking 3 parameters
print("Xrange(10,30,2) called")
for p in xrange(10,30,2):
print(p)
# showing Xrange() results on console
# Xrange() method taking 2 parameters
print("Xrange(11,20) called")
for p in xrange(11,20):
print(p)