python实现range函数
2015-07-28 20:45
585 查看
用python的代码实现range相关的功能.
结果:
#!/usr/bin/env python def r(start,end=None,step=1): if step>0: if not end: start, end = 0, start check = lambda x,y:x<y elif step<0: check = lambda x,y:x>y else: raise ValueError("range() step argument must not be zero") tmp = [] while check(start, end): tmp.append(start) start+=step return tmp print r(2,6) print r(1,10,2) print r(10,-9,-4) print r(-10) print r(1,10,0)
结果:
$ python ran.py [2, 3, 4, 5] [1, 3, 5, 7, 9] [10, 6, 2, -2, -6] [] Traceback (most recent call last): File "ran.py", line 25, in <module> print r(1,10,0) File "ran.py", line 13, in r raise ValueError("range() step argument must not be zero") ValueError: range() step argument must not be zero
相关文章推荐
- Python的面向对象、Class 概念与使用
- Python的面向对象、Class 概念与使用
- Python学习笔记-协程
- 机器学习-CrossValidation交叉验证Python实现
- [转载] Python的GIL是什么鬼,多线程性能究竟如何
- Python学习笔记-生成器
- python的字符序列处理
- python时间函数和常用格式化
- Python在windows下的安装与配置
- Python 学习笔记13:Python + wsgi + django 配置。坑爹的python3和wsgi不兼容的解决
- Parallel Python——一个简单的分布式计算系统
- python基础教程总结15——5 虚拟茶话会
- Python操作Word批量生成文章的方法
- python 问题调试1
- Python什么是值或引用函数参数
- Python 之 获取聚类中心与其支撑点的方法
- 90 行 Python 搭一个音乐搜索工具
- 一个简单的python连接池,以及DBUtil.PooledDB的使用
- [译]Python编写虚拟解释器
- python使用win32com的心得