Python built-in functions (52) -- range, python built-in 52 range

Source: Internet
Author: User

Python built-in functions (52) -- range, python built-in 52 range

English document:

range( Stop)
range( Start, Stop[, Step])
Rather than being a function, rangeIs actually an immutable sequence type, as specified ented in Ranges and Sequence Types-list, tuple, range.

Note:

1. The range function is used to generate a range object. The range type is a type that represents the Integer range.
2. you can directly input an ending integer to initialize a range type. The default starting value is 0 (including 0 ). the ending integer can be greater than 0 or less than or equal to 0, but the range object generated when it is less than or equal to 0 does not actually contain any element.
>>> A = range (5) >>> arange (0, 5) >>> len (a) 5 >>> for x in a: print (x) 01234 >>> B = range (0) # input 0, empty range object >>> len (B) 0 >>> c = range (-5) # input negative number, null range Object> len (c) 0

3. You can input a starting integer and an ending integer to initialize a range type. The generated range type contains all integers between the starting INTEGER (inclusive) and ending INTEGER (excluded.

>>> a = range(1,5)>>> arange(1, 5)>>> for x in a:print(x)1234

4. the START integer and end integer are passed in. You can also input a step value to initialize a range type. The generated range type includes the start INTEGER (inclusive) and the end INTEGER (excluded) the integer after filtering by step value.

>>> a = range(1,10,3)>>> arange(1, 10, 3)>>> for x in a:print(x)147

5. When the range type is initialized, the start integer and the end integer follow the right-open principle of the left arm, that is, include the start integer, but not the end integer.

>>> A = range (51234) >>> arange (1, 5) >>> for x in a: print (x) # contains 1, not

6. All parameters received by range must be integers. They cannot be floating-point numbers or other data types.

>>> a = range(3.5)Traceback (most recent call last):  File "<pyshell#33>", line 1, in <module>    a = range(3.5)TypeError: 'float' object cannot be interpreted as an integer>>> a = range('3.5')Traceback (most recent call last):  File "<pyshell#34>", line 1, in <module>    a = range('3.5')TypeError: 'str' object cannot be interpreted as an integer

7. range is actually an immutable sequence type. It can be used to take elements, slices, and other sequence operations, but the element value cannot be modified.

>>> A = range () >>> a [0] # element 1 >>> a [:-2] # Slice range (1, 3) >>> a [1] = 2 # modify the element value Traceback (most recent call last): File "<pyshell #38>", line 1, in <module> a [1] = 2 TypeError: 'range' object does not support item assignment

 

 

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.