【python】理解循环:for,while

先看下for结构:

#!/usr/bin/python
# -*- Coding:UTF-8 -*-

for i in range(1):
    print i

输出:

0

 

输入和输出:

#!/usr/bin/python
# -*- Coding:UTF-8 -*-

for i in range(1, 10):
    print i

#输出: 1 2 3 4 5 6 7 8 9

输入和输出:

#!/usr/bin/python
# -*- Coding:UTF-8 -*-

for i in range(1, 10, 2):
    print i,

#输出:1 3 5 7 9

总结:

range([start=0], stop, [step=1])函数的含义:

1,从start开始,到stop结束,步长是step;

2,如果range只有一个参数,那么从0开始到stop-1结束;

3,step默认步长是1,可以重新写入为2或者更多;

4,起点包含start,终点不包含stop,到stop-1截止(start <= i < stop)

posted on 2017-04-28 11:46  awildfish  阅读(195)  评论(0编辑  收藏  举报

导航