首页 > 编程语言 > 详细

python循环while和for

时间:2018-10-04 16:27:13      阅读:143      评论:0      收藏:0      [点我收藏+]

1.while循环基本语法

while 条件判断:

    执行语句

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

执行结果为

[root@oldboy 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---)

执行结果为

[root@oldboy test]# python for.py 
今
天
天
气
好
晴
朗
---end---

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

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

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

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

 

python循环while和for

原文:https://www.cnblogs.com/Forever77/p/9742370.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!