python循环while和for,pythonwhilefor,1.while循环基


1.while循环基本语法

while 条件判断:

执行语句

n=0count=0while n<=10:    if n%2==0:        print(n,end=‘   ‘)        count=count+n    n=n+1print(‘‘)print(‘0到10之间的偶数的和为:‘,count)

执行结果为

[[email protected] test]# python while.py 0   2   4   6   8   10   0到10之间的偶数的和为: 30

2.for循环基本语法

for in循环:

执行语句

test=‘今天天气好晴朗‘for v in test:    print(v)print(‘---end---‘)

执行结果为

[[email protected] test]# python for.py 今天天气好晴朗---end---

3.print语句连续输出多个结果,不同结果之间用逗号隔开

4.print输出不换行,print(‘hello‘, end=‘‘),

5.continue,在循环中如果遇到continue,则终止当前循环,后面的语句都不执行,直接进入下次循环

6.break,在循环中中如果遇到break,则整个循环语句终止

python循环while和for

评论关闭