The difference between the xrange () and range () functions in Python is used:

Source: Internet
Author: User

1.range () function:

Function Description: Range ([Start,] stop[, step]), which generates a sequence based on the range specified by start and stop and step set by step.

>>>#The range () function does not support 0 parameters... >>>range () Traceback (most recent call last): File"<stdin>", Line 1,inch<module>Typeerror:range expected at least1arguments, got 0>>>#a range () of a parameter... >>> Range (10) [0,1, 2, 3, 4, 5, 6, 7, 8, 9]>>>#range of two parameters ()... >>> Range (1,20)[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]>>>#range () function with step... >>> Range (1,20,2)[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]>>>

2.xrange () function:

Function Description: The usage is exactly the same as range, the difference is not an array, but a builder.

>>>#As can be known from the above demonstration, the range () function generates an array "[]" but generates a generator with xrange () .... >>> Xrange (10) xrange (10)>>> List (xrange (10)) [0,1, 2, 3, 4, 5, 6, 7, 8, 9]>>> xrange (1,10) xrange (1, 10)>>> list (xrange (1,10))[1, 2, 3, 4, 5, 6, 7, 8, 9]>>> xrange (1,20,2) xrange (1, 21, 2)>>> list (xrange (1,21,2))[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]>>>#xrange () also does not support 0 parameter calls... >>>xrange () Traceback (most recent): File"<stdin>", Line 1,inch<module>Typeerror:xrange () requires1-3int arguments>>>

From the above example can be known: to generate a large number sequence, with xrange will be better than range performance, because do not need to open a large amount of memory space, these two are basically in the loop when used, can be said to be deferred execution.

 for  in range (1,10): ...      Print   for in xrange (1,10): ...      Print

The results of both outputs are the same, in fact there are many differences, and range generates a list object directly:

 >>> B = Range (0,100)  >>>  Print   type (b)  <type  " list   " >>>> print  Span style= "color: #000000;" > B[0,  1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29  0, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88 1, 98, 94, 
   
     >>>" "Color: #0000ff;" style=  b[0],b[10
    ]0  10>>> 
   

Xrange does not generate a list directly, but instead returns one of the values for each call:

>>> C = xrange (1,100)print  type (c)'xrange'print  Cxrange (1, +)print c[1],c[2]

So 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.

Reference Link: http://ciniao.me/article.php?id=17

The difference between the xrange () and range () functions in Python is used:

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.