內容 / 重點 / 注意事項 / 其他或補充

sample code https://github.com/arthas989/python2022

control flow的幾個方法

為什麼要控制流程?EX:戲院票價系統

if statement

for loop

while loop

range()

nested loop

pass, break, continue

enumerate() and zip() function

coding lesson - word count

下方是依講座排序的學習過程記錄

| 41. Control Flow and If Statement | CH3內容講control flow,要做到有以下方法 1.if statement 2.loops(For / While) 3.function calls 4.Pass,Break,Continue,Range,Zip,Enumerate 5.Comprehension and Generator

為什麼要control flow? 戲院票價系統

if 判斷式符合: # 整段判斷式可換成會回傳布林值的變數 ____就執行第一段 # 要執行的段落要有____indentation elif 判斷式符合: # 可以有很多個elif ____就執行第二段 else: # 其他 ____執行最後一段

判斷式可以放什麼? == /!= / …… (Comparsion Operator) and / or / not (Logical Operator) Truthy and Falsy Values

Python不支援 switch case語法 | | | --- | --- | --- | | 42. Breakfast Program | 小程式練習 由VS Code teminal執行 由CMD執行程式

由主要判斷先寫再去改分支判斷 | | | 43. For Loop | iterable(可循環) object

for variable in iterable(string、tuple、dict、set……): ____do something here

string在for迴圈的示範

tuple在for迴圈的示範 (tuple的unpacking很方便)

dictionary在for迴圈的示範 (iterable放dictName ⇒ key,放dict.value() ⇒ value 放dict.items() ⇒ (key, value) tuple pair

set在for迴圈的示範 想測試不是iterable放在for的錯誤,一開始選了int,結果packing成tuple沒試出來

43-2
示範for單行迴圈,原來只是想在同一行印出資料,但越挖越深…… 後來找到關鍵字google [列表推導式] 不過最後發現python全攻略wilson老師在50. List Comprehensions有安排章節

學習過程記錄: https://ithelp.ithome.com.tw/articles/10211286 [列表推導式]是將冗長的迴圈列表組合語句,壓縮成一行簡短易讀的程式碼 new_list = [operation for variable in original_list if condition] operation跟variable名字要相同

list 加 for 的組合技,可以對list中的元素進行操作,例如印出list中每個元素的平方 示範傳統方式 示範列表推導式

list 加 for 加 if 判斷式 例如只想印出list中的「奇數元素」平方如何做? 示範列表推導式 加 if判斷式

| | | 44. While Loop | while (判斷式符合) ____會一直執行直到while判斷式不符合 ____要執行的段落要有____indentation

無窮迴圈測試時的危險性!! 在CMD執行,不要在VS Code執行 Ctrl + C 中止 或在工作管理員結束

while迴圈示範

何時該用loop何時該用while? 一般性結論: 知道迴圈要跑幾次時用loop,for i in range() 其他用while | | | 45. Nested Loop | 要記起來Nested Loop的運作規則 看45-1 google到的直角三角型:印出 1 12 123 1234 https://www.codesansar.com/python-programming-examples/print-1-12-123-1234-pattern.htm | | | 46. Pass, Break, Continue | pass:does nothing,可以用在不得不寫一些東西的時侯EX:空的loop、if、functions

break:loop使用,遇到會中止loop,如果有巢狀迴圈,只會跳出目前的迴圈 (若要跳出所有迴圈要用下一章的return keyword)

continue:loop使用,在loop中遇到會直接跳到下一個iteration開始執行 | | | 47. Range Function | 為什麼要用range()? 手動打0~100的list很費力,其他語言也有類似的function range(start,stop,step) start default 是 0 stop not included,若只是寫一個參數就是stop 一次跳幾步,default 是 1 ,可以是 -1 會往回跳

returns an interable object 很常跟loop搭配 print(range(100)) # range(0,100) | | | 48. Improvement of Range function | Python 2 與 Python 3 在range()的運作方式不同 Python 2 pre-created a list Python 3 【當loop走訪到需要的元素時才建立物件】 【而且只需要start,stop,step,current num】 improves 【efficiency】 and 【memory usage】 所以把range() 再轉回list不是很好的做法 | | | 49. Enumerate and Zip Function | enumerate(iterable,start=0) 枚舉,會自動組成touple (counter,item) https://iter01.com/646775.html enumerate()在for迴圈的示範

zip() 可以接受 iterable items,把它他合併在一起 例如 zip(x,y,z) 三個list,若x只有2個元素,y有4個,z有3個 用loop印出zip裡的元素只會印出以x為基準的2組 zip()在for迴圈的示範 | | | 50. List Comprehensions | 範例看43-2 要做一個新的sequences(list、set、dict……沒有tuple)提供一個較有Pythonic的寫法(短、簡潔) new_list = [operation for variable in original_list if condition] operation跟variable名字要相同 | | | 51. Dict, Set Comprehension and Generator | 範例看43-2 dict comprehension new_dict = {key:value(operation) for variable in original_dict if condition} set comprehension new_set = {operation for variable in original_set if condition} generator comprehension 用( ) 與list comprehension最大不同是return一個 generator object 而且不預先分配記憶體,執行效率較好,像是range() method generator object的運用方式可再用for取出裡頭的元素 | | | 52. Coding Lesson - Word Count | find /c /v "" myfile.txt https://www.ifreesite.com/wordcount/ 線上字數計算 https://www.udemy.com/course/python-master/learn/lecture/31616540#questions/17449538

為什麼 line_count = len(lines) - 1? wilson老師在52節問與答有提到 Linux系統當中的wc command在計算行數的方式跟我們認知的有些許不同。 某些計算行數的演算法會把所有文件後方的空白列全部不放入計算, 有些會全部用1來計,有些會全部計算(像是我課程中演示的方法)。 | | | | | |

Object_name.__doc__