Python3:input()函数



Python2.x中range()函数:

  老规矩,help!

1
2
3
4
5
6
7
8
9
10
11
12
>>> 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.

  可以看出,在Python2.x中,range()返回的是可以用来迭代的列表。

Python3.x中range()函数:

  同样,help!

1
2
3
4
5
6
7
8
9
10
11
12
>>> help(range)
Help on class range in module builtins:

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).

  啊哦,在Python3.x中,range()返回的是一个range对象。

样例对比:

Python2.x:

1
2
>>> range(1,10)
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Python3.x:

1
2
3
4
>>> range(1,10)
range(1, 10)
>>> list(range(1,10))
[1, 2, 3, 4, 5, 6, 7, 8, 9]

  通过list强制类型转换,可以在Python3.x中实现Python2.x中的range函数一样的效果。

系列教程持续发布中,欢迎订阅、关注、收藏、评论、点赞哦~~( ̄▽ ̄~)~

完的汪(∪。∪)。。。zzz

0%