If you want to develop programs that can be executed on both Python 2 and Python 3, you should utilize the range method. The range() returns a range object (a type of iterable), while xrange() returns a generator object that can only be used to loop through integers. The only specific range is presented on demand, leading to the term “lazy evaluation”. Both are used in various ways and have different qualities. The return type, memory, operation usage, and performance are all factors to consider. Let’s discuss each factor with a corresponding example for better understanding.
Here’s some Python code that compares range() with xrange() in terms of the return type. First, we have initialized range() and xrange() with “one” and “two”, respectively. Finally, we put the “one” and “two” types to the test:
two = xrange(20000)
print ("Return type of range() is given below: ")
print ("Return type of xrange() is given below: ")
Here you can see the return type of range() and xrange():
Now, we are going to discuss another factor, and that is memory. The variable holding the range produced by range() takes up more memory than the variable containing the range created by xrange(). This is because range() gives a list, whereas xrange() gives an xrange() object. The following Python code compares range() with xrange() in terms of memory. We used range() to initialize “one” and an xrange to initialize “two”. After that, we used the function sys.getsizeof to check the size of “one” and “two”. The system module in Python has a function called sys.getsizeof() that returns the memory size of the object in bytes. Rather than the memory consumption that the object references, the memory usage of the item is considered. Because this method is platform-specific, it returns the expected results when built-in objects are supplied to it. Otherwise, third-party extensions may provide incorrect results. The result reveals that range() consumes more memory, whereas xrange() consumes less:
one = range(20000)
two = xrange(20000)
print ("The size using range() is : ")
print ("The size using xrange() is : ")
This is the result of the previous code:
Because range() produces a list, it can be used with any operation that can be applied to a list. However, because xrange() returns an xrange object, actions related to lists cannot be performed on it, which is a drawback. This code compares range() with xrange() in terms of operations. We have used range() and xrange() to initialize “one” and “two”, identical to the previous examples. Then, we used range() and xrange() to test the slice operation and printed the results. As can be seen, the xrange() throws an error:
two = xrange(1,6)
print ("After slicing with range, the list looks like this: : ")
print ("After slicing with xrange, the list looks like this: : ")
Here, you can refer to the attached result of the previous code example:
Because it examines only the generating object that includes only the values required by slow evaluation, xrange() is faster to implement than range(). Remember before executing the programs listed above: If you want to write code that works in both Python 2 and Python 3, use range() instead of the xrange method, which is deprecated in Python 3. The range() is faster when iterating in the same sequence numerous times. Range() will have genuine integer objects, whereas xrange() will have to reconstruct the integer object every time.
Python has two routines or functionalities for producing lists or sometimes a range of integers. These can be used for loops. The two functions that we are talking about are xrange and range. Only if you are using Python 2.x and Python 3 will the range() and xrange() comparison be useful. It is because Python 3.x’s range() method is merely a re-implementation of Python 2.x’s xrange() method. It has the same functionality as the xrange. In terms of functionality, xrange and range are essentially the same. They both provide you the option of generating a list of integers that you may use any way you want. Range and xrange are identical except range produces a Python list object, whereas xrange provides an xrange object. We learned about range() and xrange in this session. We have also included some sample programs to show you how to put these programs and methods into practice on your own. We hope you found this article helpful. Check out more Linux Hint articles for tips and tutorials.