python之循环语句与注释

 1 #while循环
 2 #for循环
 3 #break语句
 4 #continue语句
 5 # 注释
 6 #return 是函数结束语句
 7 #break  是循环结束的语句
 8 
 9 #while 求和  1-100的和
10 #求和函数
11 # def get_sum():
12 #     sum = 0 #结果和
13 #     cnt = 1 #从1开始累积
14 #     while cnt <= 100:
15 #         sum +=cnt
16 #         cnt+=1
17 #     return sum
18 # print(get_sum())
19 # print("函数结束了")
20 
21 #给函数指定的范围求和,增加形参和实参 ,如果形参是 变量=值,这叫缺省参数,缺省就是如果没传值,就是默认缺省值
22 # 如果要求奇数,可以在形参后面加一个步数 :def get_sum(start,end,step):
23 #偶数可以取余%来找出偶数,进行求和
24 def get_sum(start,end,step):
25     sum = 0 #结果和
26     cnt = start #从1开始累积
27     while cnt <= end:
28         sum +=cnt
29         cnt +=step #循环变量 求奇数或偶数和
30         #cnt+=1 #按顺序求和
31     return sum
32 print(get_sum(1,10,2))
33 print("函数结束了")
34 
35 #while 死循环
36 # while True:
37 #     print('press ctrl+C to exit')
38 
39 #for 循环 ,遍历操作,如果后面是空列表,是不会执行的
40 # students = ['jack','mary','tom']
41 # for name in students:
42 #     print(name)
43 #
44 # for one in range(0,5): #左含右不含,如果要设置步数,可以在后面再加个step  range(0,5,2)
45 #     print(one)
46 
47 #while 循环和 for 循环的区别,使用场景
48 #while 循环:根据条件次数,不知道循环次数,有条件满足就结束循环,
49 # while True:
50 #     if break:
51 #for 循环: 需要遍历操作的,需要指定循环的次数
52 
53 #break语句
54 # for one in range(0,5):
55 #     if one == 2:
56 #         break #终止,跳出循环
57 #     print(one)
58 
59 #continue
60 # for one in range(0,5):
61 #     if one == 2:
62 #         continue #结束本次循环,继续下次循环,当等于2时,继续循环,但是不打印2
63 #     print(one)

 

posted @ 2021-02-19 21:09  小boboa  阅读(222)  评论(0编辑  收藏  举报