Similarities and differences between xrange and range in Python

Source: Internet
Author: User

These two are basically used at the time of the cycle.  forIinchRange0, -):p rint i forIinchXrange0, -):p rint I the results of both outputs are the same, in fact there are many differences, and range generates a list object directly: a= Range (0, -) print type (a) print aprint a[0], a[1while Xrange does not generate a list directly, it returns one of the values in each call a= Xrange (0, -) print type (a) print aprint a[0], a[1so the performance of Xrange cycle is better than range, especially when the return is very big! Try to use xrange, unless you are going to return a list. 

Range Function Description: Range ([Start,] stop[, step]), generating a sequence based on the range specified by start and stop and step set by step. Range Example:>>> Range (5)     [0,1,2,3,4]     >>> Range (1,5)     [1,2,3,4]     >>> Range (0,6,2)    [0,2,4]xrange Function Description: The usage is exactly the same as range, the difference is not an array, but a builder. xrange Example:>>> xrange (5) xrange (5)    >>> List (xrange (5))    [0,1,2,3,4]    >>> xrange (1,5) xrange (1,5)    >>> List (xrange (1,5))    [1,2,3,4]    >>> xrange (0,6,2) xrange (0,6,2)    >>> List (xrange (0,6,2))    [0,2,4] By the above example: to generate a 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, both of which are basically used in the loop: forIinchRange0, -): Print i forIinchXrange0, -): Print I The results of both outputs are the same, in fact there are many differences, and range generates a list object directly: a= Range (0, -) print type (a) print a print a[0], a[1] Output results:<type'List'>    [0,1,2,3,4,5,6,7,8,9,Ten, One, A, -, -, the, -, -, -, +, -, +, A, at, -, -, -, -, -, in, -, to, +, -, the, *, $,Panax Notoginseng, -, the, +, A, the, +, -, $, $, -, -, the, -,Wuyi, the, -, Wu, -, About, $, -, -, -, A, +, the, -, $, the, the, the, the, -, in, the, the, About, the, the, the, +, -, the,Bayi, the, the, -, -, the, the, the, the, -, the, the, the,94, the, the, the,98, About]    0 1The xrange does not generate a list directly, but instead returns one of the values for each call: a= Xrange (0, -) print type (a) print a print a[0], a[1] Output results:<type'xrange'>xrange ( -)    0 1so xrange do loop performance better than range, especially when returning very large, try to use Xrange bar, unless you are going to return a list. 

Similarities and differences between xrange and range in Python

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.