Xrange usage analysis in Python _python

Source: Internet
Author: User

This example describes the Xrange usage in Python. Share to everyone for your reference. Specifically as follows:

Let's look at the following examples:

>>> 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 (MOS T recent call last):
  File "<stdin>", line 1, in <module>
Indexerror:list index out of range
RA Nge ([Start,] stop [, step])->list of integers

Range () returns a list of numbers that are incremented or decremented, with the element value of the list determined by three parameters

Start represents the value at which the list starts, and defaults to "0".

Stop indicates the value of the end of the list, which is an indispensable parameter

The parameter step represents the stride size, and the default value is 1.

Range () returns a list of numbers that are incremented or decremented.

Xrange is a class that returns a Xrange object. traversal using xrange (), which returns only one value per traversal. Range () returns a list that evaluates and returns all values at once. Therefore, the execution efficiency of xrange () is higher than range ()

I hope this article will help you with your Python programming.

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.