python for循环语句怎么写


本文摘自php中文网,作者藏色散人,侵删。

python for循环语句怎么写?

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

for循环的语法格式如下:

1

2

for iterating_var in sequence:

   statements(s)

实例:

1

2

3

4

5

6

7

8

9

10

11

#!/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!"

以上实例输出结果:

1

2

3

4

5

6

7

8

9

10

当前字母 : P

当前字母 : y

当前字母 : t

当前字母 : h

当前字母 : o

当前字母 : n

当前水果 : banana

当前水果 : apple

当前水果 : mango

Good bye!

通过序列索引迭代

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

实例

1

2

3

4

5

6

7

8

#!/usr/bin/python

# -*- coding: UTF-8 -*-

  

fruits = ['banana', 'apple''mango']

for index in range(len(fruits)):

   print '当前水果 :', fruits[index]

  

print "Good bye!"

以上实例输出结果:

1

2

3

4

当前水果 : banana

当前水果 : apple

当前水果 : mango

Good bye!

以上实例我们使用了内置函数 len() 和 range(),函数 len() 返回列表的长度,即元素的个数。 range返回一个序列的数。

循环使用 else 语句

在 python 中,for … else 表示这样的意思,for 中的语句和普通的没有区别,else 中的语句会在循环正常执行完(即 for 不是通过 break 跳出而中断的)的情况下执行,while … else 也是一样。

实例

1

2

3

4

5

6

7

8

9

10

11

#!/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, '是一个质数'

以上实例输出结果:

1

2

3

4

5

6

7

8

9

10

10 等于 2 * 5

11 是一个质数

12 等于 2 * 6

13 是一个质数

14 等于 2 * 7

15 等于 3 * 5

16 等于 2 * 8

17 是一个质数

18 等于 2 * 9

19 是一个质数

相关推荐:《Python教程》

以上就是python for循环语句怎么写的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

Python中正则表达式的应用讲解

Python中关于列表list的整数操作与字符操作以及矩阵操作的实例分析

Python shell是什么

Python入门学习的流程分享

Python中应用的小案例分享

Python中hashlib加密模块的分析(代码实例)

Python实现逐行分割大txt文件的方法介绍

Python中del函数的用法

Python中双冒号::是什么?

Python 类对象和实例对象动态添加方法

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...