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 Python xrange Works?
Xrange function is minimal than range function, as it works in the lazy way of execution. Suppose we have a big range of values, and if we are executing 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 range function is more as compared to xrange function as it returns the xrange object.
There is a disadvantage of using the xrange function as compared to the range function, as 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 immutable sequence of numbers, generally used in case of loops. Arguments that we are passing to 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 default for increment values, and if we don’t pass the start argument, then 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 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 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 and 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 it will take 0 as a default starting value, and the increment value will be 1 as default as we haven’t specified step also. 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 value, and it will 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 xrange function, and we have passed all the three parameters, i.e. start step and index parameters. 2 is the starting value, so 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 index and 2.5 as step value. As you can see, if we defined the floating value inside the range, i.e. 2.5 and python has returned as float object error as python was expecting an integer value and it has failed to interpret 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, end value as -10. So range function will only iterate till value 10 and step is -2. So it will be like 0, 0 (+) -2, 0 + (2)*-2, 0 + (2)*-3 …
The xrange is a very useful function; it takes 3 values as arguments, starts end and step. The step must not be zero; otherwise, it will generate python error, and arguments should be integer values; floating-point numbers won’t work. XRange function output XRange Object and can be used only inside the loop.
This is a guide to Python xrange. Here we discuss the Introduction and how python xrange works, along with different examples and its code implementation. You may also look at the following articles to learn more –