[完]Python函数 range()和arange()

  • range(start, end, step),返回一个list对象,起始值为start,终止值为end,但不含终止值,步长为step。只能创建int型list。
  • arange(start, end, step),与range()类似,但是返回一个array对象。需要引入import numpy as np,并且arange可以使用float型数据。
>>> import numpy as np
>>> range(1,10,2)
[1, 3, 5, 7, 9]
>>> np.arange(1,10,2)
array([1, 3, 5, 7, 9])
>>> range(1,5,0.5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: range() integer step argument expected, got float.
>>> np.arange(1,5,0.5)
array([ 1. ,  1.5,  2. ,  2.5,  3. ,  3.5,  4. ,  4.5])

猜你喜欢

转载自blog.csdn.net/namelessml/article/details/52431570