我是靠谱客的博主 着急纸鹤,这篇文章主要介绍python中循环迭代语句_python条件与循环-循环,现在分享给大家,希望可以做个参考。

1 while语句

while用于实现循环语句,通过判断条件是否为真,来决定是否继续执行。

1.1 一般语法

语法如下:

while expression:

suite_to_repeat

1.2 计数

1 2

3 >>> cnt =04

5 >>> while(cnt < 9):6

7 ... print 'the index is', cnt8

9 ... cnt += 1

10

11 ...12

13 the index is014

15 the index is 1

16

17 the index is 2

18

19 the index is 3

20

21 the index is 4

22

23 the index is 5

24

25 the index is 6

26

27 the index is 7

28

29 the index is 8

代码块里包含print和自增语句,将被重复执行,知道cnt不下于9。

1.3 无限循环

while True:

suite_to_repeat

条件表达式一直为真。

2  for语句

for语句是python提供的另一个循环机制,可以用于遍历序列,可以用在列表解析和生成器表达式中。

2. 1 一般语法

最后

以上就是着急纸鹤最近收集整理的关于python中循环迭代语句_python条件与循环-循环的全部内容,更多相关python中循环迭代语句_python条件与循环-循环内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(98)

评论列表共有 0 条评论

立即
投稿
返回
顶部