Python函数 range()和arange()的区分

作者:namelessml
来源:CSDN
原文:https://blog.csdn.net/namelessml/article/details/52431570
版权声明:本文为博主原创文章,转载请附上博文链接!


range(start, end, step),返回一个list对象,起始值为start,终止值为end,但不含终止值,步长为step。只能创建int型list。
arange(start, end, step),与range()类似,但是返回一个array对象。需要引入import numpy as np,并且arange可以使用float型数据。

 1 >>> import numpy as np
 2 >>> range(1,10,2)
 3 [1, 3, 5, 7, 9]
 4 >>> np.arange(1,10,2)
 5 array([1, 3, 5, 7, 9])
 6 >>> range(1,5,0.5)
 7 Traceback (most recent call last):
 8   File "<stdin>", line 1, in <module>
 9 TypeError: range() integer step argument expected, got float.
10 >>> np.arange(1,5,0.5)
11 array([ 1. ,  1.5,  2. ,  2.5,  3. ,  3.5,  4. ,  4.5])
原文地址:https://www.cnblogs.com/psztswcbyy/p/9963825.html