码迷,mamicode.com
首页 > 编程语言 > 详细

廖雪峰网站:学习python基础知识—循环(四)

时间:2018-07-23 11:06:11      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:int()   基础   python基础   mic   sum   ...   基础知识   条件   网站   

一、循环


1、for

names = [Michal, Bob, tracy]
for name in names:
    print(name)

sum = 0
for x in [1, 2, 3, 4, 5, 6, 7,8,9,10]:
    sum = sum + x
    print(sum)

# 打印数字 0 - 9
for x in range(10):
    print(x)

 

2、while

sum = 0
n = 99
while n > 0:
    sum = sum + n
    n = n - 2
    print(sum)




n = 1
while n <= 100:
    if n > 10:  # 当n = 11时,条件满足,执行break语句
        break    # break语句会结束当前循环
    print(n)
    n = n + 1
print("end")

n = 0
while n < 10:
    n = n + 1
    if n % 2 == 0:  # 如果n是偶数,执行continue语句
        continue   # continue语句会直接继续下一轮循环,后续的print()语句不会执行
    print(n)


# 计算1+2+3+...+100:
sum = 0
n = 1
while n <= 100:
    sum = sum + n
    n = n + 1
print(sum)


# 计算1x2x3x...x100:
acc = 1
n = 1
while n <= 100:
    acc = acc * n
    n = n + 1
print(acc)

 

廖雪峰网站:学习python基础知识—循环(四)

标签:int()   基础   python基础   mic   sum   ...   基础知识   条件   网站   

原文地址:https://www.cnblogs.com/qy1234/p/9352962.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!