python基础--程序交互、格式化输出、流程控制、break、continue (3)

while 后面的else 作用是指,当while 循环正常执行完,中间没有被break 中止的话,就会执行else后面的语句

count = 0 while count <= 5 : count += 1 print("Loop",count) else: print("循环正常执行完啦") print("-----out of while loop ------")

输出

# Loop 1 # Loop 2 # Loop 3 # Loop 4 # Loop 5 # Loop 6 # 循环正常执行完啦 # -----out of while loop ------

如果执行过程中被break啦,就不会执行else的语句啦

count = 0 while count <= 5 : count += 1 if count == 3:break print("Loop",count) else: print("循环正常执行完啦") print("-----out of while loop ------")

输出

# Loop 1 # Loop 2 # -----out of while loop ------ # 例子1: i=1 #用来控制行数 while i<=5: #用来控制每一行中的列数 j = 1 while j<=5: print("*", end="") #j = j+1#c语言中向让j加上1的方式: j++; ++j; j+=1; j=j+1; j+=1 print("") i = i+1 # 输出的结果为: # ***** # ***** # ***** # ***** # ***** # 例子2 i = 1 while i<=5: ''' #从键盘中输入一个值,这个值用来控制这行中*的个数 num = int(input("请输入这个行里的*的个数:")) j = 1 while j<=num: print("*", end="") j+=1 ''' j = 1 while j<=i: print("*", end="") j+=1 print("") i+=1 # 输出的结果为: # * # ** # *** # **** # ***** # 例子3 i = 1 while i<=9: j = 1 while j<=i: print("%d*%d=%d\t"%(j,i,i*j), end="") j+=1 print("") i+=1 # 输出的结果为: # 1*1=1 # 1*2=2 2*2=4 # 1*3=3 2*3=6 3*3=9 # 1*4=4 2*4=8 3*4=12 4*4=16 # 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 # 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 # 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 # 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 # 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81 # 例子4 打印1-100之间的偶数 i = 1 while i<=100: #if i是个偶数: if i%2==0: print(i) i+=1 # 例子5 打印1-100之间的前20个偶数 i = 1 num = 0 while i<=100: #if i是个偶数: if i%2==0: print(i) num+=1 if num==20: #break的作用 用来结束while循环, #即 如果在while执行的过程中,不想循环了,可以用break来做到这个效果0: break i+=1

(5)流程控制之--for...else... # 这里补充一个知识点是in 和not in,见名知意即可 # in 和 not in 见名知意即可,在或者不在 # s1 = '学生edu' # print('学' in s1) # True # print('学生' in s1) # True # print('学ed' in s1) # False # print('学ed' not in s1) # True # for循环的讲解 s1 = '写博客大师最无聊的人:周乾' ''' 写 博 客 大 师 ''' # while循环的使用 # index = 0 # while index < len(s1): # print(s1[index]) # index += 1 # for循环的使用 ''' for循环是有限循环,可以再有限的循环的步骤里面做完相应的事情 for 变量 in iterable pass ''' length = len(s1) print(length) # 输出的是长度 # for循环的使用 for i in s1: print(i) # for循环与break 和 continue 结合使用 # for else : while else:用法是一样的 # for i in 'afdsf': # print(i) # break continue s1 = 'fadadnasj' # for i in s1: # # print(i) # if i == 'a': # continue # print(i) # 不打印,但是也循环了,每次continue就跳到下一次循环了 for i in s1: continue print(i) (6)break的用法

break的用法:break是结束循环。遇见break,我们就退出循环,终止循环。也可以用break语句在循环结构终止本层循环体,从而提前结束本层循环。

flag = True print(111) while flag: print('痒') print('社会摇') print('喜洋洋') break print('我要这铁棒有何用') print(222) # 输出的结果为: # 111 # 痒 # 社会摇 # 喜洋洋 # 222 i = 1 while i<=5: print("-----") if i==3: break print(i) i+=1 # 输出的结果为: # ----- # 1 # ----- # 2 # ----- count = 0 while count <= 5 : count += 1 if count == 3:break print("Loop",count) else: print("循环正常执行完啦") print("-----out of while loop ------") (7)continue的用法

continue的用法:continue是循环。遇见continue,我们就,终止本次循环。continue语句的作用是跳过本次循环体中余下尚未执行的语句,立即进行下一次的循环条件判定,可以理解为仅结束本次循环。

flag = True print(111) while flag: print('痒') print('社会摇') print('喜洋洋') continue print('我要这铁棒有何用') print(222) count = 0 while count < 10: count = count + 1 if count == 7: continue print(count) # 输出的结果为: # 1 # 2 # 3 # 4 # 5 # 6 # 8 # 9 # 10 count = 0 while count <= 100 : count += 1 if count > 5 and count < 95: #只要count在6-94之间,就不走下面的print语句,直接进入下一次loop continue print( count) # 输出的结果为: # 1 # 2 # 3 # 4 # 5 # 95 # 96 # 97 # 98 # 99 # 100 # 101 (8)break和continue的区别

break:终止循环

continue:终止本次循环

break和continue的区别:break会跳出当前循环,也就是整个循环都不会执行了。而continue则是提前结束本次循环,直接继续执行下次循环。

(8)基本运算符

运算符

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpyxsw.html