Updated June 12, 2023
Introduction to Python xrange
Python xrange function is an inbuilt function of Python 2. It is no longer available in Python 3. It is a repeated function of the range in Python. The range function returns the list, while the xrange function returns the object instead of a list. XRange function works in a very similar way as a range function. It returns the list of values having the same syntax as the range function. If we want to generate the list of numbers, then we can use the xrange function and print those values using for loop.
How does Python xrange Works?
The xrange function is more minimal than the range function, as it works in a lazy way of execution. Suppose we have an extensive range of values, and if we are executing the range function, then it will generate a huge list of the number, and it will be saved into the memory while we might not be using all the values of the list, but in the case of xrange function, it will only generate value when it is called. Data is only generated on demand; that is why it is also called a lazy way of evaluation. Memory consumed by the range function is more than the xrange function as it returns the xrange object.
A disadvantage of using the xrange function compared to the range function is that it does not return list type. So we cannot perform list type of operations on it, but we can do it on range function.
a = xrange(start, stop, step)
XRange function generates outputs an immutable sequence of numbers, generally used in the case of loops. Arguments we pass to the range constructor must be of integer type. It can be built in an ‘int’ or object that implements the __index__ special method. XRange function performs all kinds of sequence operations except concatenation and repetition.
If we don’t pass the step method, it will take 1 as the default for increment values, and if we don’t pass the start argument, it will be 0 as a default value.
a = range(10) x = xrange(10) print(type(a)) print(type(x))
As you can see, we have a range function and xrange function, and in both functions, we have passed only a single parameter. If we check the type of both functions, the range function returns as a list, and xrange returns the xrange object.
a = range(10) x = xrange(10) print(a) print(x)
If we try to print both functions, as you can see, you get the result of the range function, but the xrange returns only the object, not the values. We can only use xrange inside the loop.
Examples of Python xrange
Examples of Python xrange are given below:
for i in xrange(7): print(i)
In the above program, we have created an xrange inside the loop. We haven’t specified any start value, so that it will take 0 as a default starting value, and the increment value will be 1 as default as we haven’t specified the step. So 7 will be treated as an index, as we can see from the output list, starting from 0 and ending at 6. Value is index position 7 only. So the end value ends – 1. Step argument cannot take 0 or any floating-point value. 0,1,2,…,end – 1 => 0,1,2,3,4
for i in xrange(2,8): print(i)
In the above program, we passed the starting value and the index parameter in the xrange function. So the execution will start from 2 values and end at the index position 8. We haven’t passed any step value, so by default, the step is considered as 1 by default.
start, start+1, start+2,…….,end-1 =>
for i in xrange(2,10,2): print(i)
In the above program, we have the xrange function, and we have passed all the three parameters, i.e., start step and index parameters. 2 is the starting value so that the value will be printed from 2. 10 is the index value, so according to the index position 9 comes on 10th position, but 9 will not be printed because we have a step of 2 that is printing only even numbers starting from 2.
Using Floating Values in Range Function
for i in xrange(1,10.5,2): print(i)
We have defined 1 as the starting value in the above program, 10 as the index, and 2.5 as the step value. As you can see, if we defined the floating value inside the range, i.e., 2.5, and Python returned a float object error as Python was expecting an integer value, and it failed to interpret the floating value into an integer value.
Reversing Range Function
for i in xrange(4,-10,-2): print(i)
In the above program, we have used 4 as a start and end value as -10. So range function will only iterate till the value is 10, and the step is -2. So it will be like 0, 0 (+) -2, 0 + (2)*-2, 0 + (2)*-3 …
The xrange is a handy function; it takes 3 values as arguments start, end, and step. The step must not be zero; otherwise, it will generate a Python error, and arguments should be integer values; floating-point numbers won’t work. The XRange function outputs an XRange Object, which you can only use inside a loop.
This is a guide to Python xrange. Here we discuss the Introduction, how Python xrange works, and different examples and its code implementation. You may also look at the following articles to learn more –