--- title: python讀書會(四) tags: python讀書會 notes: 複習、出題、keywords、陣列、程式練習、圖片處理、套件... --- {%hackmd @themes/orangeheart %} ## 4.0 回顧... 1. 迴圈內的跳出和跳過(```break```、```continue```) 2. 異步加速迴圈運算(```async```、```await```) 3. 偵錯(```except```、```raise```、```try```、```finally```) 4. 特定功能的關鍵字(```assert```、```with```、```yield```) ## 4.1 keywords ![](https://i.imgur.com/O0hr6JM.png =500x) #### 4.1.1 global, nonlocal ```global```用在一個函數裡面。 當你在函數裡面創造一個變數時,就像是牛頓將自己寫的微積分放在抽屜裏面,只有打開看才知道你創了這個變數;當你使用了```global```後,相當於將微積分發表出去,全世界的人才知道你創造了這個變數。 - [ ] 程式碼 4.1.1a ```python= #####我們先弄出一堆函數 #在函數外部宣告"常數"本身就全域了 a = [6, 3, 2] #直接在函數內輸出"常數a" def output_1(): print(a, "\n") def output_2(): print(b, "\n") #為什麼這個函數會出錯呢? #讓"常數a"直接全域!! def global_constant(): global a a = 3 #即便你要改變資料型態也是可以的歐~ print(a, "\n") #在函數裡面設定"常數a",沒有全域 def constant(): a = True print(a, "\n") output_1()#到這個函式裡面就恢復原本的全域狀態了 #如果我們將a全域,而且還讓它變成變數呢? def variable(c): global a a = c print(a, "\n") #其他 def recursion(): global a a += ( a**2 ) print(a, "\n") ''' def global_after(s): a = s global a #要先global才能使用a喔~ print(a, "\n") ''' ##############以下開始輸出######### count = 0 ############## count += 1 print(count, ".") output_1() ############## count += 1 print(count, ".") try: output_2() except: print("b還沒定義\n") ############## count += 1 print(count, ".") constant() ############## count += 1 print(count, ".") global_constant() ############## count += 1 print(count, ".") constant() ############## count += 1 print(count, ".") variable(2) ############## count += 1 print(count, ".") recursion() recursion() recursion() recursion() ``` ```nonlocal```用在一個函數裡面的函數裡面🙃。 跟```global```很像,只是```global```讓整個程式都知道了變數,```nonlocal```只有讓上一級的知道,其他的特性你可以自己嘗試看看~~ - [ ] 程式碼 4.1.1b ```python= def outer_function(): a = 5 def inner_function(): nonlocal a a = 10 print("Inner function: ",a) inner_function() print("Outer function: ",a) outer_function() print(a)##希望你輸出的數字會是3263442 XDDD ``` #### 4.1.2 del, pass, lambda * ```del```就是刪除變數。 * ```pass```幾乎是很沒用,用來表示沒用的東西;假設我們有一個尚未實現的功能,但我們希望將來實現它,就可以放在函式裡面占空間,~~以後高級嗆別人就可以用pass~~。 * 當你執行一下**程式碼4.1.2c**時會發生錯誤,明明函數```number()```回傳值型態是整數(integer),想作為```numnum()```函式的兩個變數卻行不通... 這時候,```lambda```匿名變數就可以滿足你的這項要求,使用起來像是函數,卻沒有函數的限制!!! - [ ] 程式碼 4.1.2a ```python= a = b = [3, 2, 1] print(a) print(b) del a print(a)#哎呀,a不存在了 print(b) ``` - [ ] 程式碼 4.1.2b ```python= def function(args):#單單只有這一行會出現IndentationError def function(args): pass ``` - [ ] 程式碼 4.1.2c ```python= def number(): return 5 def numnum(number(), b): print(number()+b) ``` ```double = lambda x: x * 2```用起來的效果就像是: ```python def double(x): return x * 2 ``` **補充~:fire:** ###### :book:函數句法:```filter(function, iterable)``` - [ ] 程式碼 4.1.2d ```python= #filter函數搭配lambda my_list = [1, 5, 4, 6, 8, 11, 3, 12] new_list = list(filter(lambda x: (x%2 == 0) , my_list)) print(new_list) ``` ###### :book:函數句法:```map(function, iterable)``` - [ ] 程式碼 4.1.2e ```python= my_list = [1, 5, 4, 6, 8, 11, 3, 12] new_list = list(map(lambda x: x * 2 , my_list)) print(new_list) ``` **耶~終於將keywords都解決掉了** ![](https://i.imgur.com/oTlefrg.gif =650x) --- ## 4.2 命名規則 1. 不論是常數(constant)還是變數(variable),名稱應由小寫字母(a 到 z)或大寫字母(A 到 Z)或數字(0 到 9)或下劃線 ( _ ) 組合而成,而且不能用數字開頭!! 2. **創建一個有意義的名稱。** 雖然大家幾乎是物理系的人類,但想說一句:物理變數名稱真的亂七八糟,~~希望寫程式的時候不要善用你的物理魂。~~ 3. 命名風格: :::warning * snake_case * MACRO_CASE * flatcase * kebab-case * camelCase * PascalCase ::: 4. 盡可能使用大寫字母來聲明一個常數。(像是:PI=3.1415926...、SPEED_OF_LIGHT=299792458...) ## 4.3 數字 #### 4.3.1 數字類型 程式中,數字有不同的類型,可以依照進位制、數值型態來區分。 - [ ] 程式碼 4.3.1 ```python= a = 0b1010 #Binary Literals b = 100 #Decimal Literal c = 0o310 #Octal Literal d = 0x12c #Hexadecimal Literal #Float Literal float_1 = 10.5 float_2 = 1.5e2 #Complex Literal x = 3.14j print(a, b, c, d) print(float_1, float_2) print(x, x.imag, x.real) ``` #### 4.3.2 數值轉換 將```float```和```int```型態相加,最後會得到```float```,在```float```中,精確度只到小數點後第15位(16位是不精確的)。 - [ ] 程式碼 4.3.2a ```python print(type(1 + 2.0)) ``` - [ ] 程式碼 4.3.2b ```python= print(int(2.3)) print(float(5)) print(complex('3+5j'))#string轉成complex ``` - [ ] 程式碼 4.3.2c ```python (1.1 + 2.2) == 3.3#執行看看,結果為什麼是這樣的 ``` - [ ] 程式碼 4.3.2d ```python= import decimal print(decimal.Decimal(0.1)) ``` - [ ] 程式碼 4.3.2e ```python= import math print(math.pi) print(math.cos(math.pi)) print(math.exp(10)) print(math.log10(1000)) print(math.sinh(1)) print(math.factorial(6)) ``` - [ ] 程式碼 4.3.2f ```python= import random print(random.randrange(10, 20)) x = ['a', 'b', 'c', 'd', 'e'] print(random.choice(x)) random.shuffle(x) print(x) print(random.random()) ``` #### 4.3.3 tuple, list, set, dictionary - [ ] 程式碼 4.3.3 ```python= fruits = ["apple", "mango", "orange"] numbers = (1, 2, 3) alphabets = {'a':'apple', 'b':'ball', 'c':'cat'} vowels = {'a', 'e', 'i' , 'o', 'u'} print(fruits) print(numbers) print(alphabets) print(vowels) ``` ## Homework 4 [ZeroJudge](https://zerojudge.tw) * 嘗試看看用```lambda```和```filter()```函數來解決閏年平年的判斷問題: <font color = gray> a004: 文文的求婚 </font> ## Exercise成果 用```lambda```和```filter()```函數來解決閏年平年的判斷問題。 - [x] 程式碼 姓名 ```python= x = int(input()) l = [x] new_list = list(filter(lambda x:(x%100 != 0 and x%4 == 0 or x%400 == 0 and x%4 == 0), l)) if new_list == [x]: print("leap year") else: print("common year") ```