首頁  >  文章  >  後端開發  >  python3.5如何使用range函數

python3.5如何使用range函數

silencement
silencement原創
2019-06-26 11:30:552431瀏覽

python3.5如何使用range函數

作用
產生一系列整數,傳回一個range物件

語法:

range(start,end,step)
range(start,end)
range(end)
range函數中帶有三個參數:start、end、step。
例如:產生數字0-1的列表:

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

三種情況輸出結果相同
start 起始值(包含),end 終止值(不包含),step 步長。

range(start,end)-省卻step時,預設步長為1;range(end)-省卻step、start時,預設步長為1,起始值為0
注意:step的值不能為0或浮點數

>>> list(range(2,10,2))
[2, 4, 6, 8]
>>> list(range(2,10,2.5))
Traceback (most recent call last):
  File "", line 1, in 
    list(range(2,10,2.5))
TypeError: 'float' object cannot be interpreted as an integer

以上是python3.5如何使用range函數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn