Difference Between range() and xrange() Functions in Python?


The range() method in Python is used to return a sequence object. It is used in Python 3.x The xrange() is used to generate sequence of number and used in Python 2.x. Therefore, there’s no xrange() in Python 3.x.

Let us first learn about range() and xrange() one by one.

The range() method in Python

The range() method returns a sequence of numbers and has 3 parameters i.e. start, stop and step. Here’s the syntax −

range(start, stop, step)

Here,

  • start − Integer specifying at which position to start.

  • stop − Integer specifying at which position to stop.

  • step − Integer specifying increment i.e. skip steps

Create a sequence of numbers with range()

Example

We will create a sequence here using the range() method −

# Using the range() to get sequence of numbers # Defines start and stop parameters a = range(2, 8) for n in a: print(n)

Output

2
3
4
5
6
7

Create a sequence of numbers with range() and set the step

Example

We will create a sequence here using the range() method and will also set the skip step −

# Using the range() to get sequence of numbers # Defined start, stop and step parameters a = range(2, 10, 3) for n in a: print(n)

Output

2
5
8

Create a sequence with range() and get the size of object

Example

The getsizeof() method is used to get the size of the given object in bytes −

import sys # Using the range() to get sequence of numbers # Defined start and stop parameters a = range(2, 5) for n in a: print(n) # Get the size print("Size = ",sys.getsizeof(a))

Output

2
3
4
Size = 48

The xrange() method in Python

The xrange() method returns the generator object and is somewhat similar to the range() method in Python.

Note − xrange() runs only in Python 2.x. Will throw and error in Python 3.x.

Syntax

Here’s the syntax −

range(start, stop, step)

Here,

  • start − Integer specifying at which position to start.

  • stop − Integer specifying at which position to stop.

  • step − Integer specifying increment i.e. skip steps

Let us see some examples.

Note − We ran the below programs in Python 2.x

Create a sequence of numbers with xrange()

Example

We will create a sequence here using thex range() method 

# Python 2.x # Using the range() to get sequence of numbers # Defines start and stop parameters a = range(4, 8) for n in a: print(n)

Output

4
5
6
7

Create a sequence of numbers with xrange() and set the step

Example

We will create a sequence here using the xrange() method and step will also be set 

# Python 2.x # Using the xrange() to get sequence of numbers # Defined start, stop and step parameters a = xrange(4, 12, 2) for n in a: print(n)

Output

4
6
8
10

Create a sequence with xrange() and get the size of object

Example

The getsizeof() method is used to get the size of the given object in bytes −

#Python 2.x import sys # Using the xrange() to get sequence of numbers # Defined start and stop parameters a = xrange(2, 5) for n in a: print(n) # Get the size print("Size = ",sys.getsizeof(a))

Output

2
3
4
('Size = ', 40)

The above shows the xrange() takes only 40 bytes. In the previous section we saw for the same number of elements, the range() method took 48 bytes. Therefore, xrange(0 takes less memory.

range vs xrange()

Now, let us see the difference −

Basis range() xrange()
Meaning The range() method returns a sequence of numbers i.e. a list of integers. The range() method returns a generator object
Python Version Works in Python 3.x Works in Python 2.x
Consumes More Memory (example shown above) Consumes Less Memory (example shown above)
Operations It returns a list of number therefore we can perform Arithmetic Operations. Arithmetic Operators aren’t possible on the xrange() method
Speed of Execution Slower than xrange() Faster than range()

Updated on: 15-Sep-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements