[Comparison of Python]range and xrange usage

Source: Internet
Author: User

The following are the details of the "collation":

The

First looks at the following example:
>>> x=xrange (0,8)
>>>  print x
Xrange (8)
>>> print x[0]
0
>>> print x[7]
7
> >> print x[8]
Traceback (most recent call last): File ' <stdin> ', line 1, in <module>indexerror : Xrange object index out of range
>>>  x=range (0,8)
>>>   Print x
[0, 1, 2, 3, 4, 5, 6, 7]
>>>  print x[0]
0
>>>  print X[8]
Traceback (most recent call last): File ' <stdin> ', line 1, in <module>indexerror:list Inde X out of Rangerange ([Start,] stop [, step])->list of integers

 

 

Range (start, stop, Step) returns a list of numbers that are incremented or decremented, and the element value of the list is determined by three parameters:

Start indicates the value of the start of the list, which can be saved by default of "0"
Stop represents the value of the end of the list, which is not a missing parameter
The parameter step indicates the step size, which can be saved by the default value of "1"
Range returns a list of all values that are calculated and returned at once

Xrange is a class that returns a generator:
The generator is an iterative object that is created individually when the generator is iterated.
Use Xrange () to traverse, returning only one value per traversal
In general, when iterating over a large sequence, because of the characteristics of the xrange, it will save more memory
As a result, xrange is more efficient than range

First, let's look at Python 2 range (). It is a built-in function that is used to create integer arithmetic progression. Therefore, it is often used for a for loop.

From the official help document, we can see the following features:
1. Built-in function (built-in)
2, accept 3 parameters are start, stop and step (where start and step are optional, stop is required)
3. If start is not specified, the default starts at 0 (Python starts from 0)
4. If no step is specified, the default step is 1. (Step cannot be 0, if you specify step 0, "valueerror:range () step argument must not is zero" will be thrown)

Next look at Xrange (). Xrange () is a built-in function, but it is defined as a type in Python, and this type is called xrange. We can easily see this in the Python 2 interactive shell.

We can see from Xragne's official help document that:

The parameters and usages of xrange and range are the same. Just Xrange () returned is no longer a sequence, but a Xrange object. This object can generate numbers (that is, elements) within the specified range of parameters as needed.

Because the Xrange object generates a single element on demand, instead of creating the entire list first, as in range. So, within the same range, Xrange takes up less memory space and xrange is faster.

In fact, Xrange generates an element because it is called within a loop, so no matter how many times it is looped, only the current element occupies memory space, and each loop consumes the same single element space.

We can roughly think that the same n elements, range occupies a space of xrange n times. Therefore, in a large cycle, the efficiency and speed of the xrange will be apparent.

Summarize:
1, Range () returns the entire list.
2, Xrange () returns a Xrange object, and this object is a iterable.
3, both can be used for the For loop.
4. Xrange () takes up less memory space because xrange () only generates the current element at loop time, unlike the range () to generate a complete list at the beginning.

This is the same point and difference between range and xrange in Python 2.

Range () and xrange () are two different implementations in Python 2. However, in Python 3, the implementation of range () is removed, the implementation of xrange () is preserved, and Xrange () is renamed to Range ().

[Comparison of Python]range and xrange usage

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.