函数(1)

1.一个小脚本,重点在它写法。

def interval(start,stop=None,step=1):
    if stop is None:   #如果没有值,定义的比较完善
        start,stop = 0,start
    result = []
    i = start               #运行机制
    while i < stop:
        result.append(i)
        i +=step
    return result

print(interval(5))
print(interval(1,10,2))

执行结果:
[0, 1, 2, 3, 4]     #其实就是列表的切片
[1, 3, 5, 7, 9]
原文地址:https://www.cnblogs.com/yangmingxianshen/p/7759183.html