主要是利用生成器来写的一个函数:

def myxrange(start, stop = None, step = 1):     #这里的stop一定要等于None,不能等于0,要不然会有好多问题
if stop == None:
stop = start
start = 0
while start < stop:
yield start
start += 1
elif stop != None and step == 1:
while start < stop:
yield start
start += 1
else:
if stop < start and step < 0:
while start > stop:
yield start
start += step
elif stop >= start and step > 0:
while start < stop:
yield start
start += step
else:
print("不合法")
for x in myxrange(10, 0, -3):
print(x,end = " ")
for x in myxrange(1, 10):
print(x, end = " ")
for x in myxrange(10):
print(x, end = " ")
print(sum(x**2 for x in myxrange(1, 10) if x % 2 != 0 ))

输出结果:

10 7 4 1

1 2 3 4 5 6 7 8 9

0 1 2 3 4 5 6 7 8 9

165

模仿python中的range功能的更多相关文章

  1. 使用Java迭代器实现Python中的range

    如果要想迭代一个类的对象,那么该类必须实现 Iterable 接口,然后通过 iterator 方法返回一个 Iterator 实例. Range 类实现了Python中的range的所有用法,如:r ...

  2. Python中的range和xrange区别

    range 函数说明:range([start,] stop[, step]),根据start与stop指定的范围以及step设定的步长,生成一个序列. range示例: >>> r ...

  3. Python中set的功能介绍

    Set的功能介绍 1.集合的两种函数(方法) 1. 集合的内置函数 交集 格式:x.__and__(y)等同于x&y 例如:s1 = {'a',1,} s2 = {'b',1,} s3 = { ...

  4. Python中dict的功能介绍

    Dict的功能介绍 1. 字典的两种函数(方法) 1. 字典的内置函数 包含关系 格式:x.__contains__(key)等同于key in x 例如:dic = {'ab':23,'cd':34 ...

  5. Python中tuple的功能介绍

    Tuple的功能介绍 1. 元祖的两种方法 1. 元祖的内置方法 两个元祖的相加 格式:x.__add__(y)等同于x+y 例如:tu1 = (1,2,3,) print(tu1.__add__(( ...

  6. Python中list的功能介绍

    List的功能介绍 1. 列表的两种方法 1. 列表的内置方法 列表的相加 格式:x.__add__(y)等同于x+y 例如:list1 = [1,2,3] print(list1.__add__([ ...

  7. python中int的功能简单介绍

    Int的功能介绍 1. 绝对值 x.__abs__()等同于abs(x) 2. 加法 x.__add__(y)等同于x+y 3. 与运算 x.__and__(y)等同于x&y 4. 布尔运算 ...

  8. python中的range与xrange

    range 也是一种类型(type),它是一个数字的序列(s sequence of numbers),而且是不可变的,通常用在for循环中. class range(stop) class rang ...

  9. Python中的range函数用法

    函数原型:range(start, end, scan): 参数含义:start:计数从start开始.默认是从0开始.例如range(5)等价于range(0, 5); end:技术到end结束,但 ...

随机推荐

  1. C++输入/输出流

    2017-08-17 09:03:28 writer:pprp 基本的输入/输出流 默认情况下,输入操作会丢弃前导空白,读取数据,遇到空白的时候停止读入: 如果希望的如包括空白在内的任意字符,可以使用 ...

  2. Centos 查看端口占用情况

    netstat -ntlp 把相应PID kill掉即可

  3. WebBrowser与console.log()

    在WebBrowser中,页面上的console.log()会影响后续代码的执行. <script>console.log(1); alert("1"); //不执行 ...

  4. 如何理解nRF5芯片外设PPI

    PPI,英文全称Programmable Peripheral Interconnect,是Nordic独有的外设,其设计目的是让CPU处于idle模式下外设与外设之间也能完成相应通信,从而降低系统功 ...

  5. 终于搞懂了shell bash cmd...

    问题一:DOS与windows中cmd区别 在windows系统中,“开始-运行-cmd”可以打开“cmd.exe”,进行命令行操作. 操作系统可以分成核心(kernel)和Shell(外壳)两部分, ...

  6. 分享海量 iOS 及 Mac 开源项目和学习资料

    UI 下拉刷新 EGOTableViewPullRefresh - 最早的下拉刷新控件. SVPullToRefresh - 下拉刷新控件. MJRefresh - 仅需一行代码就可以为UITable ...

  7. iometer测试磁盘IO性能

    of Outstanding I/Os per target – 被选中worker的每个磁盘一次所允许的未处理的异步I/O的数量.模拟测试多个应用向 IO 请求读写,默认是 1.通常不用这个参数,除 ...

  8. 【Demo】jQuery 轮播图简单动画效果

    功能实现: (1)设定图片称号的鼠标悬停事件: (2)在事件中利用自定义动画函数调整显示图片,并修改对应标号样式: (3)为图片显示区域设定鼠标悬停事件: (4)当鼠标停在该区域时,清除图片切换动画定 ...

  9. Oracle登录被拒绝; 权限不足或用户名/口令无效

    第一步: 打开CMD命令窗,输入如下命令:sqlplus "/as sysdba",回车 第二步: 输入命令:alter user sys identified by Orcl12 ...

  10. linux-git shell colors

    git config --global color.status auto git config --global color.diff auto git config --global color. ...