一、if判斷 語法一: 二、while迴圈 三、for迴圈 ...
一、if判斷
語法一:
if條件:
代碼塊1
代碼塊2
代碼塊3
1 示例: 2 sex='female' 3 age=18 4 is_beautiful=True 5 if sex =='female'and age>16 and age<20 and is_beautiful: 6 print('開始表白。。。') 7 else: 8 print('阿姨好。。。') 9 10 print('other code1...') 11 print('other code2...') 12 print('other code3...') 13 14 執行結果:第一個條件通過則輸出:開始表白、other code1......,第一個條件不通過則輸出:阿姨好
1 示例: 2 sex='female' 3 age=18 4 is_beautiful=True 5 if_sex =='female'and age>16 and age<20 and is_beautiful: 6 print('開始表白。。。') 7 print('other code1...') 8 print('other code2...') 9 print('other code3...') 10 執行結果:開始表白、other code1.....
語法二:
if條件:
代碼塊1
代碼塊2
代碼塊3
else:條件不成立時執行的子代碼塊
代碼塊1
代碼塊2
代碼塊3
1 示例: 2 sex='female' 3 age=18 4 is_beautiful=True 5 if sex =='female'and age>16 and age<20 and is_beautiful: 6 print('開始表白。。。') 7 else: 8 print('阿姨好。。。') 9 10 print('other code1...') 11 print('other code2...') 12 print('other code3...') 13 14 執行結果:第一個條件通過則輸出:開始表白、other code1......,第一個條件不通過則輸出:阿姨好
語法三:
if條件1:
if條件2:
代碼塊1
代碼塊2
代碼塊3
1 示例: 2 sex = 'female' 3 age = 18 4 is_beautiful = True 5 is_sucessful=True 6 height=1.70 7 8 if sex == 'female' and age > 16 and age < 20 and is_beautiful and height >1.60 and height <1.80: 9 print('開始表白。。。') 10 if is_sucessful: 11 print('在一起。。。') 12 else: 13 print('什麼愛情不愛情的。。。。。') 14 else: 15 print('阿姨好。。。') 16 17 print('other code1...') 18 print('other code2...') 19 print('other code3...')
1 # 註意在代碼過長的情況下可以使用\來進行分隔: 2 # is_beautiful=\ 3 # True 4 # print(is_beautiful) 5 # 輸出結果:True 6 ''' 7 sex = 'female' 8 age = 18 9 is_beautiful = True 10 is_sucessful=True 11 height=1.70 12 13 if sex == 'female' and age > 16 and age < 20 and is_beautiful \ 14 and height >1.60 and height <1.80: 15 print('開始表白。。。') 16 if is_sucessful: 17 print('在一起。。。') 18 else: 19 print('什麼愛情不愛情的。。。。。') 20 else: 21 print('阿姨好。。。') 22 23 print('other code1...') 24 print('other code2...') 25 print('other code3...')
語法四:
if條件1:
代碼塊1
代碼塊2
代碼塊3
elif條件2:
代碼塊1
代碼塊2
代碼塊3
elif條件3:
代碼塊1
代碼塊2
代碼塊3
else:
代碼塊1
代碼塊2
代碼塊3
1 示例:如果:成績>=90,那麼:優秀 2 3 如果成績>=80且<90,那麼:良好 4 5 如果成績>=70且<80,那麼:普通 6 7 其他情況:很差 8 9 score=input('請輸入成績/please input your score: ') 10 score=int(score) 11 if score>=90: 12 print('優秀') 13 elif score>=80 and score<90: 14 print('良好') 15 elif score>=70 and score<80: 16 print('普通') 17 else: 18 print('很差')
二、while迴圈
語法:
while 條件
代碼1
代碼2
代碼3
1 示例: 2 while True: 3 name=input('please input your name:') 4 pwd=input('please input your password:') 5 6 if name =='egon' and pwd =='123': 7 print('login successful') 8 else: 9 print('username or password error')註:現這個迴圈是在持續運行中
1 方式一:條件改為False,在條件改為False時不會立即結束掉迴圈,而是要等到下一次迴圈判斷條件時才會結束。 2 示例: 3 tag=True 4 while tag: 5 name=input('please input your name:') 6 pwd=input('please input your password:') 7 8 if name =='egon' and pwd =='123': 9 print('login successful') 10 tag=False 11 else: 12 print('username or password error') 13 print('===>>>')
1 方式二:while+break 2 break一定要放在迴圈體內,一旦迴圈體執行到break就會立即結束本層迴圈 3 4 示例: 5 while True: 6 name=input('please input your name:') 7 pwd=input('please input your password:') 8 9 if name =='egon' and pwd =='123': 10 print('login successful') 11 break #運行正確則結束本層迴圈 12 else: 13 print('username or password error') 14 15 print('===>>>>') # (這個無法執行)
1 方式三: 2 while+continue: 3 結束本次迴圈,直接進入下一次迴圈 4 5 count=1 6 while count<6: #count=1,2,3,4, 7 if count == 4: 8 continue 9 print(count) 10 count +=1 #現執行結果在4這個數進行死迴圈 11 12 count=1 13 while count<6: #count=1,2,3,4,5 14 if count == 4: 15 count += 1 16 continue 17 print(count) 18 count += 1 19 20 while True: 21 name=input('please input your name:') 22 pwd=input('please input your password:') 23 24 if name =='egon' and pwd =='123': 25 print('login successful') 26 break #運行正確則結束本層迴圈 27 else: 28 print('username or password error') 29 continue #結束本次迴圈 30 print('===>>>>') # (這個無法執行)
三、for迴圈
1 for迴圈的強大之處在於迴圈取值 2 l=['a','b','c','d','e'] 3 i=0 4 while i< len(l): 5 print(l[i]) 6 i +=1 7 8 for x in l: #x='b' 9 print(x)
1 dic={'name':'egon','age':18,'gender':'male'} 2 for x in dic: 3 print(x,dic[x])
1 1、for+else 2 names=['egon','kevin111_dsb','alex_dsb','mac_dsb'] 3 for name in names: 4 if name=='kevin_dsb': 5 break 6 print(name) 7 else: 8 print('=======>') # 執行結果是正常執行完所有姓名並結束 9 ''' 10 ''' 11 names=['egon','kevin_dsb','alex_dsb','mac_dsb'] 12 for name in names: 13 if name=='kevin_dsb': 14 break 15 print(name) 16 else: 17 print('=======>') # 執行結果是輸出egon 18 ''' 19 '''
1 2、for+continue 2 3 nums=[11,22,33,44,55] 4 for x in nums: 5 if x == 22 or x == 44: 6 continue 7 print(x) #執行結果是11、33、55
1 3、for+range() 2 3 range的用法 (cmd中執行結果) 4 >>> range(1,5) 5 [1, 2, 3, 4] 6 >>> for i in range(1,5): 7 ... print(i) 8 ... 9 1 10 2 11 3 12 4 13 >>> range(1,5,1) 14 [1, 2, 3, 4] 15 >>> range(1,5,2) # 1 3 16 [1, 3]
1 示例:for i in range(1,5,2): #range是只顧頭不顧尾,2代表步長指1,1+2=3,3+2=5(不包含5所以不輸出) 2 print(i) # 執行結果是1,3 3 4 for i in range(5) # >>>>0,1,2,3,4 5 print(i) # >>>>0,1,2,3,4
1 4、for嵌套 2 for i in range(3): # >>>>i=0,1,2 3 for j in range(4):# >>>>j=0,1,2,3 4 print(i,j) 5 6 # 最終執行結果: 如i=0(不變)j=0 0 0 1 0 2 0 7 i = 0(不變)j=1 0 1 1 1 2 1 8 i = 0(不變)j=2 0 2 1 2 2 2 9 i = 0(不變)j=3 0 3 1 3 2 3