資訊內容
python中range函數(shù)用法是什么
python中range函數(shù)用法:【range()】函數(shù)可創(chuàng)建一個整數(shù)列表,一般用在for循環(huán)中,函數(shù)語法為【range(start, stop[, step])】,其中start是開始計數(shù),stop是停止,step是步長。SDu少兒編程網(wǎng)-https://www.pxcodes.com
SDu少兒編程網(wǎng)-https://www.pxcodes.com
本教程操作環(huán)境:windows7系統(tǒng)、python3.9版,DELL G3電腦。SDu少兒編程網(wǎng)-https://www.pxcodes.com
python中range函數(shù)用法:SDu少兒編程網(wǎng)-https://www.pxcodes.com
range()函數(shù)可創(chuàng)建一個整數(shù)列表,一般用在 for 循環(huán)中。SDu少兒編程網(wǎng)-https://www.pxcodes.com
注意:Python3 range() 返回的是一個可迭代對象(類型是對象),而不是列表類型, 所以打印的時候不會打印列表。SDu少兒編程網(wǎng)-https://www.pxcodes.com
函數(shù)語法SDu少兒編程網(wǎng)-https://www.pxcodes.com
range(start, stop[, step])參數(shù)說明:SDu少兒編程網(wǎng)-https://www.pxcodes.com
start: 計數(shù)從 start 開始。默認是從 0 開始。例如range(5)等價于range(0, 5);SDu少兒編程網(wǎng)-https://www.pxcodes.com
stop: 計數(shù)到 stop 結束,但不包括 stop。例如:range(0, 5) 是[0, 1, 2, 3, 4]沒有5SDu少兒編程網(wǎng)-https://www.pxcodes.com
step:步長,默認為1。例如:range(0, 5) 等價于 range(0, 5, 1)SDu少兒編程網(wǎng)-https://www.pxcodes.com
實例SDu少兒編程網(wǎng)-https://www.pxcodes.com
>>>range(10) # 從 0 開始到 10 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> range(1, 11) # 從 1 開始到 11 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> range(0, 30, 5) # 步長為 5 [0, 5, 10, 15, 20, 25] >>> range(0, 10, 3) # 步長為 3 [0, 3, 6, 9] >>> range(0, -10, -1) # 負數(shù) [0, -1, -2, -3, -4, -5, -6, -7, -8, -9] >>> range(0) [] >>> range(1, 0) []以下是 range 在 for 中的使用,循環(huán)出runoob 的每個字母:SDu少兒編程網(wǎng)-https://www.pxcodes.com
>>>x = 'runoob' >>> for i in range(len(x)) : ... print(x[i]) ... r u n o o b >>>相關免費學習推薦:python視頻教程SDu少兒編程網(wǎng)-https://www.pxcodes.com
以上就是python中range函數(shù)用法是什么的詳細內容,更多請關注少兒編程網(wǎng)其它相關文章!SDu少兒編程網(wǎng)-https://www.pxcodes.com

- 上一篇
Python中的裝飾器是什么?裝飾器是如何工作的?
簡介Python很早就引入了裝飾器——在PEP-318中,作為一種簡化函數(shù)和方法定義方式的機制,這些函數(shù)和方法在初始定義之后必須進行修改。這樣做的最初動機之一是,使用classmethod和staticmethod等函數(shù)來轉換方法的原始定義,但是它們需要額外的一行代碼來修改函數(shù)的初始定義。一般來說,每次
- 下一篇
python怎樣判斷字符串是否為整數(shù)
簡介python判斷字符串是否為整數(shù)的方法:1、遞歸法,可以先根據(jù)字符串的第一個字符確定整數(shù)的正負,接著對字符串從右向左遍歷;2、非遞歸法,實現(xiàn)方法為從左向右遍歷字符串計算整數(shù)的值。本教程操作環(huán)境:windows7系統(tǒng)、python3.9版,DELLG3電腦。python判斷字符串是否為整數(shù)的方法:方法