Python基礎(3)if_else、for、while、break與continue
阿新 • • 發佈:2018-07-12
作用 ger 一次 style pri while font 天涯 正常
1、if ... else
1 a=6 2 if a>=5: 3 print("The a is bigger than 5") 4 else: 5 print("The a is smaller than 5")
2、for循環
1 #for i in range(10):#默認從0開始,步進為1相當於c語言for(i=0;i<10;i++) 2 for i in range(1,10,3):#從1開始,步進為3 3 print("loop:", i )
3、while循環
1 count=0 2 while True: 3 print("你是風兒我是沙,纏纏綿綿到天涯...",count) 4 count +=1
4、break與continue
1 #break用於完全退出本層循環 2 while True: 3 print ("break:123") 4 break 5 print( "456") 6 7 #continue用於退出本次循環,繼續下一次循環 8 while True: 9 print( "continue:123") 10 continue 11 print( "456")
5、while+else
#與其它語言else 一般只與if 搭配不同,在Python 中還有個while ...else 語句,while 後面的else 作用是指,
當while 循環正常執行完,中間沒有被break 中止的話,就會執行else後面的語句
Python基礎(3)if_else、for、while、break與continue