流程控制-for循环(序列)
循环
循环是一个结构,导致程序要重复一定的次数。 条件下循环也是如此,当然条件变为假,循环结束。
for循环:
在序列里,使用for循环遍历。
语法:
for iterating_var in sqquence: statement(s)
举例:
(例1)for用法举例
In [1]: a = 'ABC'In [2]: a Out[2]: 'ABC'In [3]: for i in a: ...: print i ...: A B C In [4]: for i in a: print i, #加逗号显示在同一行用空格隔开。...: A B C
(例2)list的for循环
In [5]: list1 = [1,3,4,5] In [6]: list1 Out[6]: [1, 3, 4, 5] In [7]: for i in list1: ...: print i ...: 1 3 4 5
(例3)range()函数用法:
In [11]: range(1,11) Out[11]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] In [12]: range(11) Out[12]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] In [13]: range(1,11,2) Out[13]: [1, 3, 5, 7, 9] #脚本:打印出1到10的10个数字:#!/usr/bin/pythonfor i in range(1,11): print i运行结果: [root[@localhost](https://my.oschina.net/u/570656) python]# python for.py 1 2 3 4 5 6 7 8 9 10 [root[@localhost](https://my.oschina.net/u/570656) python]#
(例4)print加","显示为一行
脚本1: #!/usr/bin/python print [i for i in range(1,11)] 运行结果: [root[@localhost](https://my.oschina.net/u/570656) python]# python for.py [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] [root[@localhost](https://my.oschina.net/u/570656) python]#脚本2: #!/usr/bin/python print [i*2 for i in range(1,11)] 运行结果: [root[@localhost](https://my.oschina.net/u/570656) python]# python for.py [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] [root@localhost python]#脚本3 求1,10内的偶数: #!/usr/bin/python print [i for i in range(1,11) if i%2==0] 运行结果: [root@localhost python]# python for.py [2, 4, 6, 8, 10] [root@localhost python]#
(例5)列表重写:
脚本4 求1,10内的偶数的乘方: #!/usr/bin/python for i in [i**2 for i in range(1,11) if i%2==0]: print i, 运行结果: [root@localhost python]# python for.py 4 16 36 64 100 [root@localhost python]#脚本5 求1到100所有数加到一起的和: #!/usr/bin/python sum = 0 for i in range(1,101): sum = sum + i print sum 运行结果: [root@localhost python]# python for1.py 5050
(例6)xrange()
产生的是对象,节省内存: xrange(10) In [3]: a = xrange(20) In [4]: type(a) Out[4]: xrange In [6]: for i in a: print i, ...: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
流程控制-for循环(字典)
生成字典:
In [14]: dic = dic.fromkeys('abcdef','100') In [15]: type(dic) Out[15]: dict In [16]: dic Out[16]: {'a': '100', 'b': '100', 'c': '100', 'd': '100', 'e': '100', 'f': '100'}
(例1)打印出键值:
In [18]: for k in dic: ....: print k ....: a c b e d f
(例2)打印出键值和value
In [19]: for k in dic: ....: print k,dic[k] ....: a 100 c 100 b 100 e 100 d 100 f 100
格式化输出
In [21]: for k in dic: print "%s --->> %s " % (k,dic[k]) ....: a --->> 100c --->> 100b --->> 100e --->> 100d --->> 100f --->> 100
(例3)items()方法:
dic.items():返回的是列表 : In [22]: dic.items() Out[22]: [('a', '100'), ('c', '100'), ('b', '100'), ('e', '100'), ('d', '100'), ('f', '100')] In [23]: for i in dic.items():print i ('a', '100') ('c', '100') ('b', '100') ('e', '100') ('d', '100') ('f', '100') In [24]: for k,v in dic.items():print k,v a 100 c 100 b 100 e 100 d 100 f 100
(例4)dic.iteritems() 方法:
dic.iteritems() 返回的是对象: In [25]: for k,v in dic.iteritems():print k,v a 100 c 100 b 100 e 100 d 100 f 100
一个print 可以输出一个换行符:
(例5)举例乘法口诀:
#!/usr/bin/python for i in xrange(1,10): for j in xrange(1,i+1): print "%sx%s=%s" % (j,i,j*i), print 运行结果: [root@localhost python]# python koujun.py 1x1=1 1x2=2 2x2=4 1x3=3 2x3=6 3x3=9 1x4=4 2x4=8 3x4=12 4x4=16 1x5=5 2x5=10 3x5=15 4x5=20 5x5=25 1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36 1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49 1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64 1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81 [root@localhost python]#