### What is the difference between range and xrange in Python?

In This blog, we are going to learn, What is the difference between range and xrange?

In Python programming, The range() and xrange() are two functions that could be used to iterate a certain number of times in for loops in Python.

### xrange()

The xrange() function in Python is used to generate a sequence of numbers, similar to the range() function.

### range()

The range() is an in-built function in Python. It returns a sequence of numbers starting from zero and incrementing by 1 by default and stops before the given number.

#### Difference between range and xrange

The** range()** returns a list type object. For example, the expression range(1, 50, 1) will produce a 49 int numbers range.

```
>>> r = range(1, 50, 1)
>>> type(r)
<type 'list'>
>>> len(r)
```**Output:** 49

**xrange()** object allows iteration, indexing, and the len() method. You can have a for loop to traverse it and gets the numbers in every iteration.

```
for i in xrange(6):
print(i)
```**Output:**
0
1
2
3
4
5

```
Python range vs. xrange Conformity
1.
```**Consumption of Memory :**
range() returns a list of elements, it takes more memory.
xrange() In comparison to range(), it takes less memory.
2. **Return type : **
range() It returns a list of integers.
xrange() It returns a generator object.
3. **Speed :**
range() Its execution speed is slower.
xrange() Its execution speed is faster.

**In Python 3, there is no xrange, but the range function behaves like xrange in Python 2 **.

Thanks for reading and supporting this blog.