python的内置函数之range() 作者:马育民 • 2018-07-01 23:18 • 阅读:10046 ####range()函数生成整数序列 ####代码: ```python >range(5) ``` 显示:range(0, 5),表示生成从0到4的序列 ####使用方法1: ```python >r=range(5) >for i in r: print(i) ``` 显示: ```python 0 1 2 3 4 ``` ####使用方法2: ```python >r=range(5,10) >for i in r: print(i) ``` 生成5到9的序列 ```python 5 6 7 8 9 ``` ####使用方法3: ```python >r=range(0,10,2) >for i in r: print(i) ``` 生成0到9,步长是2的序列 ```python 0 2 4 6 8 ``` ####通过索引访问 ```python >r[1] ``` ####不能修改值 ```python >r[2]=100 ``` 错误提示如下: ```python Traceback (most recent call last): File "", line 1, in TypeError: 'range' object does not support item assignment ``` 原文出处:http://www.malaoshi.top/show_1EF1Qqfs4RuX.html