Python built-in functions (52) -- range, python built-in 52 range
English document:
-
range
(
Stop)
-
range
(
Start,
Stop[,
Step])
-
Rather than being a function,
range
Is 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
-