Range vs. Xrange Range
function Usage:
Range (stop)
Range (Start,stop[,step])
function Description:
This is a generic function that creates a list that contains a sequence of columns. It is most commonly used for a For loop. The parameter must be a normal integer. If the step parameter is omitted, the default is 1. If the start parameter is omitted, the default is 0. The full form of the function returns a list of integers [start, start + step, start + 2 * step, ...]. If step is positive, then the last element start + I * step is maximum and less than stop, if step is negative, then the last element start + I * step is the smallest and greater than stop. Step must not be 0 (otherwise the ValueError will be thrown).
Example:
>>>RangeTen)[0,1,2,3,4,5,6,7,8,9]>>>Range1, One)[1,2,3,4,5,6,7,8,9,Ten]>>>Range0, -,5)[0,5,Ten, the, -, -]>>>Range0,Ten,3)[0,3,6,9]>>>Range0,-Ten,-1)[0,-1,-2,-3,-4,-5,-6,-7,-8,-9]>>>Range0)[]>>>Range1,0)[]
Xrange
function Usage:
Xrange (STOP)
Xrange (Start,stop[,step])
function Description:
This function is very similar to range (), but it returns the Xrange object type instead of the list. This is an inert sequence type that generates the same values as the corresponding list, but does not actually store them together. Xrange () has little advantage over range (because xrange () must still create the desired value) unless a very large range or all elements of this range are used on a memory-tight machine (for example, when loops are often terminated by break).
xrange Object Type description
Xrange types are immutable sequences that are typically used for loops. The benefit of the xrange type is that the Xrange object always consumes the same amount of memory, regardless of the size of the range it represents. But it doesn't always have the same performance advantage.
Xrange objects behave infrequently: they support only the index, iteration, and Len () functions.
Example:
>>>Xrange5) Xrange (5)>>>List (xrange (5))[0,1,2,3,4]>>>Xrange1,5) Xrange (1,5)>>>List (xrange (1,5))[1,2,3,4]>>>Xrange0,6,2) Xrange (0,6,2)>>>List (xrange (0,6,2))[0,2,4]
As can be seen from the above example: Range () is exactly the same as the Xrange () method, basically in the loop:
forin range(0,10) print iforin xrange(0,10) print i
And both outputs are the same, the biggest difference is that range () generates a list object directly, and xrange () generates a Xrange object instead of a list object, each time the call returns the value.
a=range(0,10)print type(a)print aprint a[0],a[1]
The output is:
‘list‘>[0,1,2,3,4,5,6,7,8,9]0,1
b=xrange(0,10)print type(b)print bprint b[0],b[1]
The output is:
‘xrange‘>xrange(10)0,1
If you want to generate a very large number sequence, using xrange will be much better than range performance, because there is no need to open up a large amount of memory space. So, in comparison, the performance of the Xrange () loop is better than range (), especially if you need to return a list.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Comparison of function range () with xrange ()