Python程式設計入門學習筆記(四)
阿新 • • 發佈:2018-12-25
## python第二課 ### 課程內容 1、條件判斷 2、迴圈 3、函式 4、類 ### 條件判斷 ```python #偽程式碼表示 if condition: do something else: do something ``` #### 應用題:小明買水果,合計金額為32.5元,水果店搞活動,滿30打九折,求小明的實際花費? ```python total_cost = 32.5 if total_cost >30: discount = 0.9 else: discount = 1 total_cost *= discount print('小明的實際花費為: {}元'.format(total_cost)) ``` 小明的實際花費為: 29.25元 #### 應用題:如果購買水果超過30元,打九折,超過50元,打八折,求小明的實際花費? ```python total_cost = 62.5 is_vip = True if total_cost > 50: if is_vip: discount = 0.8 else: discount = 1 elif total_cost >30: discount = 0.9 else: discount = 1 total_cost *= discount print('小明的實際花費為: {}元'.format(total_cost)) ``` 小明的實際花費為: 50.0元 ### 重點 1、條件判斷可以任意組合 第一層意思:elif可以有0到任意多個,else可有可無 第二層意思:條件判斷可以進行巢狀 2、著重看一下condition ```python bool(''),bool({}),bool([]) ``` (False, False, False) ```python condition = '' if condition: print('True') else: print('False') ``` False #### 從理解的角度來講,一個值被當做布林值,概念上更像是有與沒有的區別。 and or not ### 布林型變數做運算 ```python a = True b = False print('a and b is {}'.format(a and b)) print('a or b is {}'.format(a or b)) ``` a and b is False a or b is True ### 非布林型變數做and or not 運算 ```python a = 'hello world' b = [] print(bool(b)) print('a and b is {}'.format(a and b)) print('a and b is {}'.format(a or b)) ``` False a and b is [] a and b is hello world ```python #非布林型變數 and 運算 a = [1,2,3] b = 10 print(a and b) #非布林型變數 or 運算 a = 'ni hao' b = {'apple': 100} print(a or b) #非布林型變數 not 運算,永遠返回True或者False print (not b) ``` 10 ni hao False ### 條件判斷的近親 - 斷言 ```python #虛擬碼 if not condition: crash program #它的意思是說:我斷言它肯定是這樣的,如果不是這樣,那我就崩潰。 ``` ```python age = 19 assert age == 18,'他竟然不是18歲' ``` --------------------------------------------------------------------------- AssertionError Traceback (most recent call last) <ipython-input-22-b5515fdbbb07> in <module>() 1 age = 19 ----> 2 assert age == 18,'他竟然不是18歲' AssertionError: 他竟然不是18歲 ### 迴圈 for 迴圈 - 遍歷迴圈 while迴圈 - 條件迴圈 ```python costs = [3,4,12,23,43,100] for cost in costs: print('消費 {} 元'.format(str(cost).center(10))) ``` 消費 3 元 消費 4 元 消費 12 元 消費 23 元 消費 43 元 消費 100 元 ### 生成一個長度為20的隨機列表 ```python import random random_numbers = [] while len(random_numbers) < 20: random_numbers.append(random.randint(1,10)) print(random_numbers,len(random_numbers)) ``` [3, 4, 2, 3, 1, 1, 5, 10, 3, 7, 10, 7, 6, 8, 6, 5, 6, 3, 9, 4] 20 #### 程式設計建議:只要能使用For迴圈,就不要使用while迴圈。 ```python random_numbers = [] for i in range(20): random_numbers.append(random.randint(1,10)) print(random_numbers,len(random_numbers)) ``` [9, 5, 3, 6, 1, 3, 4, 9, 8, 10, 2, 7, 10, 4, 7, 5, 7, 1, 6, 7] 20 #### 什麼時候必須用while迴圈:當迴圈的條件跟數量沒有關係時,只能用while #### 題目:往空列表中新增隨機數,直到新增的隨機數為9,則終止 ```python random_numbers = [] while (9 not in random_numbers): random_numbers.append(random.randint(1,10)) print(random_numbers,len(random_numbers)) ``` [6, 1, 10, 4, 4, 4, 3, 6, 9] 9 ### 重點:只有一個元素的列表 #### 問題:a = [1,2,3] , b = 1, c = (b in a),猜測一下c是什麼型別,它是不是一個元祖呢? ```python # 死迴圈演示 import time number = 0 while True: time.sleep(1) number += 1 print('hello world, {}'.format(number),end = '\r') ``` hello world, 54 --------------------------------------------------------------------------- KeyboardInterrupt Traceback (most recent call last) <ipython-input-36-ef3ff4317da5> in <module>() 2 number = 0 3 while True: ----> 4 time.sleep(1) 5 number += 1 6 print('hello world, {}'.format(number),end = '\r') KeyboardInterrupt: ```python a = [] b = () type(a),type(b) ``` (list, tuple) ```python a = [1] b = (1) type(a),type(b),b ``` (list, int, 1) ```python a = [1] b = (1,) type(a),type(b),b,len(b) ``` (list, tuple, (1,), 1) ```python a = [1,2,3] b = 1 c = (b in a) type(a),type(b),type(c),c ``` (list, int, bool, True) #### 上題中c不是一個元祖,它是一個布林型變數 ```python random_numbers ``` [6, 1, 10, 4, 4, 4, 3, 6, 9] #### continue 跳過 ```python for number in random_numbers: if number % 2 == 0: print('{} is 偶數'.format(number)) else: # print('{} is 奇數'.format(number)) continue print('沒有跳過') ``` 6 is 偶數 沒有跳過 10 is 偶數 沒有跳過 4 is 偶數 沒有跳過 4 is 偶數 沒有跳過 4 is 偶數 沒有跳過 6 is 偶數 沒有跳過 #### break 跳出迴圈 ```python for number in random_numbers: if number % 2 == 0: print('{} is 偶數'.format(number)) else: break print('沒有結束') ``` 6 is 偶數 沒有結束 #### 迴圈中的else:如果在迴圈過程中沒有碰到break語句,就會執行else裡面的程式碼 ```python random_numbers = [4,2,4] for number in random_numbers: if number % 2 == 0: print('{} is 偶數'.format(number)) else: break print('沒有結束') else: print('all') ``` 4 is 偶數 沒有結束 2 is 偶數 沒有結束 4 is 偶數 沒有結束 all #### for迴圈可以構建推導式 #### 所謂推導式,就是從一個數據序列構建另一個數據序列的方法 ```python random_numbers = list(range(10)) random_numbers ``` [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] ```python new_numbers = [] for number in random_numbers: new_numbers.append(number*10) new_numbers ``` [0, 10, 20, 30, 40, 50, 60, 70, 80, 90] #### 列表推導式 ```python #for迴圈構建的推導式 new_numbers = [number*10 for number in random_numbers] new_numbers ``` [0, 10, 20, 30, 40, 50, 60, 70, 80, 90] #### 字典推導式 ```python dict_numbers = {number:'A' for number in random_numbers} dict_numbers ``` {0: 'A', 1: 'A', 2: 'A', 3: 'A', 4: 'A', 5: 'A', 6: 'A', 7: 'A', 8: 'A', 9: 'A'} ```python tuple_numbers = (number*10 for number in random_numbers) tuple_numbers ``` <generator object <genexpr> at 0x0000021E4B4F6E08> #### 生成器 ```python #執行第一次和第二次的資料是不一樣的,第二次為空列表,生成器只能生成一次 tuple(tuple_numbers) ``` ()