发布时间:2019-09-27 07:09:02编辑:auto阅读(1911)
当你在不同python版本下使用 range() 时, 需要注意了
我们先在原始IDE下分别码出来:
python 2.
>>> range(2, 19) [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
python 3.
>>> range(2, 19) range(2, 19)
而这样
>>> list(range(2, 19)) [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
为什么会这样呢?
这是因为,在py2中range()是作为内置函数, 而在py3中是作为一个内置的方法
注意看以下的源代码(部分):
py2
def range(start=None, stop=None, step=None): # known special case of 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. """ pass
py3
class range(object): """ range(stop) -> range object range(start, stop[, step]) -> range object Return an object that produces a sequence of integers from start (inclusive) to stop (exclusive) by step. range(i, j) produces i, i+1, i+2, ..., j-1. start defaults to 0, and stop is omitted! range(4) produces 0, 1, 2, 3. These are exactly the valid indices for a list of 4 elements. When step is given, it specifies the increment (or decrement). """
运行环境分别为2.7.14和3.5.4
上一篇: python3 字典操作
下一篇: Python3 解释器
47750
46252
37132
34640
29231
25893
24762
19866
19426
17916
5720°
6323°
5843°
5892°
6992°
5830°
5850°
6365°
6319°
7683°