Python Built-in Types {内置类型} range
Built-in Types
https://docs.python.org/3/library/stdtypes.html
内置类型
https://docs.python.org/zh-cn/3/library/stdtypes.html
The range
type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in for
loops.
range
类型表示不可变的数字序列,通常用于在 for
循环中循环指定的次数。
class range(stop)
class range(start, stop[, step])
上面函数描述中,中括号括起来的参数是可选的。中括号是可选参数,逗号是参数之间的分隔符。
The arguments to the range
constructor must be integers (either built-in int
or any object that implements the __index__
special method). If the step
argument is omitted, it defaults to 1
. If the start
argument is omitted, it defaults to 0
. If step
is zero, ValueError
is raised.
range
构造器的参数必须为整数 (可以是内置的 int
或任何实现了 __index__
特殊方法的对象)。如果省略 step
参数,其默认值为 1。如果省略 start
参数,其默认值为 0,如果 step
为零则会引发 ValueError
。
For a positive step
, the contents of a range r
are determined by the formula r[i] = start + step * i
where i >= 0
and r[i] < stop
.
如果 step
为正值,确定 range r
内容的公式为 r[i] = start + step * i
其中 i >= 0
且 r[i] < stop
。
For a negative step, the contents of the range are still determined by the formula r[i] = start + step * i
, but the constraints are i >= 0
and r[i] > stop
.
如果 step
为负值,确定 range
内容的公式仍然为 r[i] = start + step * i
,但限制条件改为 i >= 0
且 r[i] > stop
.
A range object will be empty if r[0]
does not meet the value constraint. Ranges do support negative indices, but these are interpreted as indexing from the end of the sequence determined by the positive indices.
如果 r[0]
不符合值的限制条件,则该 range
对象为空。range
对象确实支持负索引,但是会将其解读为从正索引所确定的序列的末尾开始索引。
Ranges containing absolute values larger than sys.maxsize
are permitted but some features (such as len()
) may raise OverflowError
.
元素绝对值大于 sys.maxsize
的 range
对象是被允许的,但某些特性 (例如 len()
) 可能引发 OverflowError
。
Range examples:
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(1, 11))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> list(range(0, 30, 5))
[0, 5, 10, 15, 20, 25]
>>> list(range(0, 10, 3))
[0, 3, 6, 9]
>>> list(range(0, -10, -1))
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
>>> list(range(0))
[]
>>> list(range(1, 0))
[]
Ranges implement all of the common sequence operations except concatenation and repetition (due to the fact that range objects can only represent sequences that follow a strict pattern and repetition and concatenation will usually violate that pattern).
range
对象实现了一般序列的所有操作,但拼接和重复除外 (这是由于 range
对象只能表示符合严格模式的序列,而重复和拼接通常都会违反这样的模式)。
start
The value of the start parameter (or 0 if the parameter was not supplied)
start
形参的值 (如果该形参未提供则为 0)
stop
The value of the stop parameter
stop
形参的值
step
The value of the step parameter (or 1 if the parameter was not supplied)
step
形参的值 (如果该形参未提供则为 1)
The advantage of the range type over a regular list
or tuple
is that a range object will always take the same (small) amount of memory, no matter the size of the range it represents (as it only stores the start
, stop
and step
values, calculating individual items and subranges as needed).
range
类型相比常规 list
或 tuple
的优势在于一个 range
对象总是占用固定数量的 (较小) 内存,不论其所表示的范围有多大 (因为它只保存了 start
, stop
和 step
值,并会根据需要计算具体单项或子范围的值)。
Range
objects implement the collections.abc.Sequence ABC
, and provide features such as containment tests, element index lookup, slicing and support for negative indices (see Sequence Types - list
, tuple
, range
):
range
对象实现了 collections.abc.Sequence ABC
,提供如包含检测、元素索引查找、切片等特性,并支持负索引 (参见序列类型 - list
, tuple
, range
):
>>> r = range(0, 20, 2)
>>> r
range(0, 20, 2)
>>> 11 in r
False
>>> 10 in r
True
>>> r.index(10)
5
>>> r[5]
10
>>> r[:5]
range(0, 10, 2)
>>> r[-1]
18
Testing range
objects for equality with ==
and !=
compares them as sequences. That is, two range objects are considered equal if they represent the same sequence of values. (Note that two range objects that compare equal might have different start, stop and step attributes, for example range(0) == range(2, 1, 3)
or range(0, 3, 2) == range(0, 4, 2)
.)
使用 ==
和 !=
检测 range
对象是否相等是将其作为序列来比较。也就是说,如果两个 range 对象表示相同的值序列就认为它们是相等的。 (请注意比较结果相等的两个 range
对象可能会具有不同的 start
, stop
和 step
属性,例如 range(0) == range(2, 1, 3)
而 range(0, 3, 2) == range(0, 4, 2)
。)
The linspace recipe
shows how to implement a lazy version of range
suitable for floating point applications.
linspace recipe
演示了如何实现一个延迟求值版本的适合浮点数应用的 range
对象。
Python 3 range()
函数返回的是一个可迭代对象 (类型是对象),而不是列表类型,所以打印的时候不会打印列表。
Python 3 list()
函数是对象迭代器,可以把 range()
返回的可迭代对象转为一个列表,返回的变量类型为列表。
Python 2 range()
函数返回的是列表。
range()
和 xrange()
是 Python 2 内置函数 (Built-in Functions)
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
C:\Users\foreverstrong>python
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> range(10)
range(0, 10)
>>>
>>> type(range(10))
<class 'range'>
>>>
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>
>>> list(range(0))
[]
>>>
>>> exit()
C:\Users\foreverstrong>
References
[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/