python3.5如何使用range函数


本文摘自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+selenium开发环境教程讲解

Python怎么生成时间戳

实例详解Python基于回溯法子集树模板解决最佳作业调度

Python中关于str与repr的使用详解

Python中if语句用法

Python删除list中的重复元素

Python是强类型语言吗

Python中的seed()方法怎么用

Python如何将客户的数据一直保存

Python for语句的执行过程是什么

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...