python3.5如何使用range函数-Python教程

资源魔 37 0

作用
孕育发生一系列整数,前往一个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 "<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教程 python编程 python使用问题 range函数

抱歉,评论功能暂时关闭!