Python for 循环语句用法详解

您所在的位置:网站首页 pythonfor循环怎么写 Python for 循环语句用法详解

Python for 循环语句用法详解

#Python for 循环语句用法详解| 来源: 网络整理| 查看: 265

Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串。 语法: for循环的语法格式如下:

for iterating_var in sequence:    statements(s)

流程图:  

实例:

#!/usr/bin/python # -*- coding: UTF-8 -*- for letter in 'Python':     # 第一个实例    print '当前字母 :', letter fruits = ['banana', 'apple',  'mango'] for fruit in fruits:        # 第二个实例    print '当前水果 :', fruit print "Good bye!"

以上实例输出结果:

当前字母 : P 当前字母 : y 当前字母 : t 当前字母 : h 当前字母 : o 当前字母 : n 当前水果 : banana 当前水果 : apple 当前水果 : mango Good bye!

通过序列索引迭代 另外一种执行循环的遍历方式是通过索引,如下实例: 实例

#!/usr/bin/python # -*- coding: UTF-8 -*- fruits = ['banana', 'apple',  'mango'] for index in range(len(fruits)):    print '当前水果 :', fruits[index] print "Good bye!"

以上实例输出结果:

当前水果 : banana 当前水果 : apple 当前水果 : mango Good bye!

以上实例我们使用了内置函数 len() 和 range(),函数 len() 返回列表的长度,即元素的个数。 range返回一个序列的数。 循环使用 else 语句 在 python 中,for … else 表示这样的意思,for 中的语句和普通的没有区别,else 中的语句会在循环正常执行完(即 for 不是通过 break 跳出而中断的)的情况下执行,while … else 也是一样。 实例

#!/usr/bin/python # -*- coding: UTF-8 -*- for num in range(10,20):  # 迭代 10 到 20 之间的数字    for i in range(2,num): # 根据因子迭代       if num%i == 0:      # 确定第一个因子          j=num/i          # 计算第二个因子          print '%d 等于 %d * %d' % (num,i,j)          break            # 跳出当前循环    else:                  # 循环的 else 部分       print num, '是一个质数'

以上实例输出结果:

10 等于 2 * 5 11 是一个质数 12 等于 2 * 6 13 是一个质数 14 等于 2 * 7 15 等于 3 * 5 16 等于 2 * 8 17 是一个质数 18 等于 2 * 9 19 是一个质数

使用内置 enumerate 函数进行遍历:

for index, item in enumerate(sequence):     process(index, item)

实例

>>> sequence = [12, 34, 34, 23, 45, 76, 89] >>> for i, j in enumerate(sequence): ...     print i,j ... 0 12 1 34 2 34 3 23 4 45 5 76 6 89

使用list.append()模块对质数进行输出。

#!/usr/bin/python # -*- coding: UTF-8 -*- # 输出 2 到 100 简的质数 prime = [] for num in range(2,100):  # 迭代 2 到 100 之间的数字    for i in range(2,num): # 根据因子迭代       if num%i == 0:      # 确定第一个因子          break            # 跳出当前循环    else:                  # 循环的 else 部分       prime.append(num) print prime

输出结果:

[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

打印空心等边三角形: #!/usr/bin/python # -*- coding: UTF-8 -*- # 打印空心等边三角形

rows = int(raw_input('输入行数:')) for i in range(0, rows):     for k in range(0, 2 * rows - 1):         if (i != rows - 1) and (k == rows - i - 1 or k == rows + i - 1):             print " * ",         elif i == rows - 1:             if k % 2 == 0:                 print " * ",             else:                 print "   ",         else:             print "   ",     print "\n"

打印1-9三角形阵列:

#!/usr/bin/python # -*- coding: UTF-8 -*- for i in range(1,11):     for k in range(1,i):         print k,         k +=1     i +=1     print "\n"

输出结果:

1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 9 

Python中for循环搭配else的陷阱 假设有如下代码:

for i in range(10):     if i == 5:         print 'found it! i = %s' % i else:     print 'not found it ...'

你期望的结果是,当找到5时打印出:

found it! i = 5

实际上打印出来的结果为:

found it! i = 5 not found it ...

显然这不是我们期望的结果。 根据官方文档说法: >When the items are exhausted (which is immediately when the sequence is empty), the suite in the else clause, if present, is executed, and the loop terminates. >A break statement executed in the first suite terminates the loop without executing the else clause’s suite. A continue statement executed in the first suite skips the rest of the suite and continues with the next item, or with the else clause if there was no next item. https://docs.python.org/2/reference/compound_stmts.HTML#the-for-statement 大意是说当迭代的对象迭代完并为空时,位于else的子句将执行,而如果在for循环中含有break时则直接终止循环,并不会执行else子句。 所以正确的写法应该为:

for i in range(10):     if i == 5:         print 'found it! i = %s' % i         break else:     print 'not found it ...'

当使用pylint检测代码时会提示 Else clause on loop without a break statement (useless-else-on-loop) 所以养成使用pylint检测代码的习惯还是很有必要的,像这种逻辑错误不注意点还是很难发现的 python


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3