"Python Things" range () and xrange () __python

Source: Internet
Author: User

Whether range () or xrange () is a built-in function in Python. The two built-in functions are most commonly used in the For loop. Range () and xrange () are two different implementations in Python 2.

But in Python 3, the implementation of range () is removed, the implementation of xrange () is preserved, and the Xrange () is renamed to Range ().

First, let's look at the Python 2 range (). It is a built-in function that is used to create an integer arithmetic progression, so it is often used for the For loop.
From the official help document, we can see the following features: built-in functions (built-in), 3 parameters are start, stop and step (where start and step are optional, stop is required), or if start is not specified, The default is starting from 0 (Python starts with 0), and if you do not specify step, the default step is 1. (Step cannot be 0, if the step is specified 0, "valueerror:range () step argument must is not zero" will be thrown).

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

As you can see from the documentation, the parameters and usage of xrange and range are the same. Just Xrange () is no longer a sequence, but a Xrange object. This object can generate a number (that is, an element) within the range of parameters specified on demand. Because the Xrange object generates a single element on demand, rather than a range, the entire list is created first. So, in the same range, Xrange takes up less memory space and xrange faster. In fact, Xrange generates an element because it is called within a loop, so no matter how many times the loop is used, only the current element occupies the memory space, and each cycle occupies the same single element space. We can roughly assume that, with the same n elements, the range occupies the space of xrange n times. Therefore, in a very large cycle, xrange efficiency and rapid performance will be obvious.

To sum up: range () returns the entire list; Xrange () returns a Xrange object, which is a iterable; Both are used with the For loop; Xrange () takes up less memory space because xrange () only generates the current element at the time of the loop, unlike range () to generate a complete list at the beginning.
This is the same point and difference between range and xrange in Python 2.

Please understand the following sentence:
"The advantage of the range type over a regular list or tuple be that a Range object would always take the same (small) AMO Unt of memory, no matter the size of the The range it represents (as it only stores the start, stop and step values, Calculati ng individual items and subranges as needed). "

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.