作者: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])
