加分練習
將每個被調用的函數以上述的方式翻譯成 python 實際執行的動做。例如 ' '.join(things) 實際上是 join(' ', things) 。
將這兩種方式翻譯爲天然語言。例如 ' ".join(things) 能夠翻譯成 「用 ’ ’ 鏈接(join) things」,而 join(' ', things) 的意思是 「爲 ’ ’ 和 things 調用 join 函數」。
在網上閱讀一些關於 「面向對象編程 (Object Oriented Programming)」 的資料。不要怕看着暈,其實你們開始都暈,而《笨辦法學 python》 會教給咱們足夠的知識逐步掌握它
瞭解 Python 的 class 是什麼東西。必定是 Python 的哦
瞭解 dir(something) 和 something 的 class 有什麼關係?
Zed 建議,若是實在皋懂 面向對象編程(OOP)能夠學習如下 「函數編程 (functional programming)」
python
1 ten_things = "Apples Oranges Crows Telephone Light Sugar" 2 3 print("Wait there are not 10 things in that list. Let's fix that.") 4 5 # 字符串的 split 方法會把字符串按照參數隔開生成一個列表 6 stuff = ten_things.split(' ') 7 # print(stuff) 8 more_stuff = ["Day", "Night", "Song", "Frisbee", 9 "Corn", "Banana", "Girl", "Boy"] 10 11 # print(">>>>>> ten_things length:", len(ten_things)) 12 13 # 不斷從 more_stuff 中取出元素加入 stuff 中直到 14 # stuff 的長度等於10,確認 while 循環會正確結束 15 while len(stuff) != 10: 16 next_one = more_stuff.pop() 17 # print(more_stuff) 18 print("Adding: ", next_one) 19 stuff.append(next_one) 20 print(f"There are {len(stuff)} items now.") 21 22 print("There we go: ", stuff) 23 24 print("Let's do some things with stuff.") 25 26 print(stuff[1]) 27 print(stuff[-1]) #whoa! fancy 28 print(stuff.pop()) 29 print(' '.join(stuff)) # what? cool! 30 print('#'.join(stuff[3:5])) # super stellar!
運行結果編程
38.1 + 38.2 翻譯函數語句
stuff = ten_things.split(' ') 翻譯: ten_things 使用空格 分割爲列表
實際執行 stuff = split(ten_things, ' ' ) 翻譯: 爲 ten_things 和 調用 split 函數app
next_one = more_stuff.pop() 翻譯:more_stuff 使用 拋出方法
實際執行 pop(more_stuff) 翻譯:調用 pop 函數,參數是 more_stuff函數
stuff.append(next_one) 翻譯:爲 stuff 末尾添加 next_one
實際執行 append(stuff, next_one) 翻譯:爲 stuff 和 next_one 調用 append 方法學習
stuff.pop() 翻譯: 爲 sutff 調用 pop 方法
實際執行 pop(suff) 翻譯:調用 pop 函數,參數 suff
spa