pytho range() 函数可创建一个整数列表,一般用在 for 循环中。
Grammar:
<pre>
range(start, stop[, step])
</pre>
参数说明:
start: 计数从 start 开始。默认是从 0 开始。例如range(5)等价于range(0, 5);
end: 计数到 end 结束,但不包括 end。例如:range(0, 5) 是[0, 1, 2, 3, 4]没有5
step:步长,默认为1。例如:range(0, 5) 等价于 range(0, 5, 1)
Case:
<pre>
>>> type(range(10)) #类型:List列表
<type 'list'>
>>> help(range)
Help on built-in function range in module __builtin__:
range(...)
range(stop) -> list of integers
range(start, stop[, step]) -> list of integers
Return a list containing an arithmetic progression of integers.
range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
When step is given, it specifies the increment (or decrement).
For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!
These are exactly the valid indices for a list of 4 elements.
>>> range(5) #=range(0,5,1);从O开始,到5-1终止,步长1
[0, 1, 2, 3, 4]
>>> range(1,5) #从1开始
[1, 2, 3, 4]
>>> range(0,5,2)
[0, 2, 4]
>>> range(0,-10,-2) #负数
[0, -2, -4, -6, -8]
>>> range(0) #空列表
[]
</pre>
quote:http://www.runoob.com/python/python-func-range.html
Grammar:
<pre>
range(start, stop[, step])
</pre>
参数说明:
start: 计数从 start 开始。默认是从 0 开始。例如range(5)等价于range(0, 5);
end: 计数到 end 结束,但不包括 end。例如:range(0, 5) 是[0, 1, 2, 3, 4]没有5
step:步长,默认为1。例如:range(0, 5) 等价于 range(0, 5, 1)
Case:
<pre>
>>> type(range(10)) #类型:List列表
<type 'list'>
>>> help(range)
Help on built-in function range in module __builtin__:
range(...)
range(stop) -> list of integers
range(start, stop[, step]) -> list of integers
Return a list containing an arithmetic progression of integers.
range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
When step is given, it specifies the increment (or decrement).
For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!
These are exactly the valid indices for a list of 4 elements.
>>> range(5) #=range(0,5,1);从O开始,到5-1终止,步长1
[0, 1, 2, 3, 4]
>>> range(1,5) #从1开始
[1, 2, 3, 4]
>>> range(0,5,2)
[0, 2, 4]
>>> range(0,-10,-2) #负数
[0, -2, -4, -6, -8]
>>> range(0) #空列表
[]
</pre>
quote:http://www.runoob.com/python/python-func-range.html