1.

Distinguish between range and xrange in Python.

Answer»

xrange and range are nearly identical in terms of functionality. They both allow you to generate a list of integers that you may use whatever you WANT. The only difference between range and xrange is that xrange produces an xrange object while range provides a Python list object.

This implies that, unlike range, xrange does not create a STATIC list during EXECUTION. It generates the values as needed using a technique known as yielding. Generators, which are a type of object, are employed with this technique. That is, if you have a large range and want to construct a list for a billion people, you should use xrange.

This is particularly the case if you're working with a system that demands a lot of memory, such as a cell phone, because range will utilise as MUCH RAM as it can to build your array of numbers, causing a memory problem and crashing your app.



Discussion

No Comment Found