本文摘自php中文网,作者silencement,侵删。

作用
产生一系列整数,返回一个range对象
语法:
range(start,end,step)
range(start,end)
range(end)
range函数中带有三个参数:start、end、step。
例如:产生数字0-1的列表:
1 2 | >>> list(range(0,10,1))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
1 2 | >>> list(range(0,10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
1 2 | >>> 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或者浮点数
1 2 | >>> list(range(2,10,2))
[2, 4, 6, 8]
|
1 2 3 4 5 | >>> list(range(2,10,2.5))
Traceback (most recent call last):
File "< pyshell #16>", line 1, in < module >
list(range(2,10,2.5))
TypeError: 'float' object cannot be interpreted as an integer
|
以上就是python3.5如何使用range函数的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
Python opencv检测并提取目标颜色
Python基于opencv的图像压缩算法实例分析
Python 怎么获取网页内容
Python怎么用time模块监控程序运行时间
没学过编程可以学Python吗
Python标准库之sched模块介绍
Python中divmod函数的用法是什么?
零基础写Python爬虫之爬虫编写全记录_Python
Python是什么情况下诞生的
Python正则表达式和re库的相关内容介绍(代码示例)
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python3.5如何使用range函数