# 11/16 Python基礎程式設計(四) Python Basic Tutorial (4) > [name=王博均][time=19:00][color=black] :::warning 今天主要課程為回顧上次的迴圈==loop==,和介紹函式(==function==)與模組(==module==) Today’s main lesson is to review the last ==loop== and introduce ==functions== and ==modules==. ::: ![image](https://hackmd.io/_uploads/HJcRr9LV6.png) ![image](https://hackmd.io/_uploads/B1tk8cL46.png) :::info * list 這個 data type 可用 ==append== 將其他元素加進來 * 其他 list 的 operator 1. list.pop(index) eg. [1,2,3] list.pop(2) => 將 index = 2 的元素移除 2. list.remove(element) eg. [1,2,3] list.remove(2) => 將 element = 2 移除 * List can use ==append== to add other elements. * other list operators 1. list.pop(index) eg. [1,2,3] list.pop(2) => Remove the element with index = 2 2. list.remove(element) eg. [1,2,3] list.remove(2) => remove element = 2 ::: ![image](https://hackmd.io/_uploads/SkIIu98NT.png) :::info 此時傳入的是一個tuple(name,age),若少了括號,會變成 append 兩個元素(name/age) What is passed in is a tuple (name, age). If the brackets are missing, it will become append two elements (name/age) ::: ![image](https://hackmd.io/_uploads/BJAX59UVT.png) ![image](https://hackmd.io/_uploads/HyQVY9UNT.png) :::info 當我們在使用迴圈時,需用2個element,我們可以用zip()將兩者結合在一起。而當中的每個element都是一個tuple => (a,b) ::: ## :beginner: Function :::success 假設我們編寫了一個大型程序,可以接受特定的輸入並在處理後產生很酷的結果。 這個程式非常有用,而您會經常使用它。 在這樣的在這種情況下,您可以使用函數來簡化程式碼並使其更具可讀性。 這也是避免用重複程式碼的好習慣,因為大多數程式設計師不喜歡重複程式碼。 函數允許您將程式碼區塊封裝成可重複使用的單元,讓您的管理和維護更加輕鬆。 每當需要執行某項操作時,您都可以呼叫該函數特定任務或操作,減少冗餘並提高代碼組織。 Assuming we've written a large program that can take specific inputs and produce a cool result after processing. This large program is quite useful, and you'll use it frequently. In such cases, you can use functions to simplify your code and make it more readable. It's also a good practice to avoid repeating code, as most programmers prefer not to duplicate code. Functions allow you to encapsulate a block of code into a reusable unit, making it easier to manage and maintain your code. You can call the function whenever you need to perform a specific task or operation, reducing redundancy and improving code organization. ::: ![image](https://hackmd.io/_uploads/rkDjj9U4p.png) :::info * 冒號後的縮排範圍都是我們的fuction * 呼叫fuction時,後面要加括號 * The indentation range after the colon is our function * When calling function, add parentheses after it. ::: ![image](https://hackmd.io/_uploads/r1uIn9UVp.png) ![image](https://hackmd.io/_uploads/SyCYn58ET.png) :::info * 當我們希望函式有回傳值,我們會用==return== * c = foo(a=3, b=5) => 我們不知道參數放左邊是 a,還是右邊是 a,我們強制使函數 a=3, b=5 * a = foo(5,0) => 函式中的 a 和函式外的 a 互不影響 * When we want a function to return a value, we use ==return== * c = foo(a=3, b=5) => We don’t know whether the parameter **a** is on the left or on the right. We force the function a=3, b=5 * a = foo(5,0) => **a** in the function and **a** outside the function have no influence on each other ::: ![image](https://hackmd.io/_uploads/By_9RcIN6.png) ![image](https://hackmd.io/_uploads/H1YjRcU4a.png) ![image](https://hackmd.io/_uploads/BkJaA9U46.png) :::info * foo(3, 2, x=1) => 先用position argument將3給x,再用keyword argument將1給x,因為重複給值,所以錯誤 * 主旨: 同一個變數只能給一個值 * foo(3, 2, x=1) => First use the position argument to give 3 to x, and then use the keyword argument to give 1 to x. This is an error because the value is given repeatedly. * Purpose: The same variable can only give one value ::: ![image](https://hackmd.io/_uploads/ry2ZljLVa.png) :::info * 可先在函式中初始化參數,這樣就算沒有給參數,也可以得到結果 * You can initialize the parameters in the function first, so that you can get the result even if no parameters are given. ::: ![image](https://hackmd.io/_uploads/HkPsls8ET.png) :::info * '*'可用在不確定要加入多少值時=>將放入的所有東西做成tuple * 因為'*'會將剩下的所有傳入值取走,所以不可將變數放在其後,除非該變數default有值或用keyword argument => def foo(x, *y, z=1) * '*' can be used when you are not sure how many values ​​to add => make everything put in a tuple * Because '*' will take away all the remaining incoming values, variables cannot be placed after it unless the variable default has a value or keyword argument => def foo(x, *y, z=1) ::: ![image](https://hackmd.io/_uploads/r1LuXsLNT.png) :::info ![image](https://hackmd.io/_uploads/BkcxNoLEa.png) **result:** ![image](https://hackmd.io/_uploads/SkCbNjIVa.png) ::: ![image](https://hackmd.io/_uploads/BJYU4oU46.png) :::info 若想要換掉print的間隔,需要keyword argument eg. print(1,2,3,4,sep='yyy',end='aaa') result: 1yyy2yyy3yyy4aaa (結尾沒有換行) If you want to change the print interval, you need keyword argument eg. print(1,2,3,4,sep='yyy',end='aaa') result: 1yyy2yyy3yyy4aaa (no newline at the end) ::: ## :triangular_flag_on_post: Import :::success 您已經付出了努力來創建您的函數,現在您想讓它們可供其他人使用。 然而,俗話說,程式設計師不喜歡重複程式碼。因此,Python提供了import關鍵字,它允許您的程式碼可以利用其他文件中的函數和模組。 You've put in the effort to create your functions, and now you want to make them available for others to use. However, as the age-old saying goes, programmers don't like duplicating code. Therefore, Python provides the import keyword, which allows your code to leverage functions and modules from other files. ::: ![image](https://hackmd.io/_uploads/Sydm8iINa.png) ![image](https://hackmd.io/_uploads/rkNE8jUNT.png) :::info ![image](https://hackmd.io/_uploads/Sk6FIjLEp.png) ::: ![image](https://hackmd.io/_uploads/SJOhLjLNT.png) ![image](https://hackmd.io/_uploads/Bke6Us8ET.png) :::info * 從函式庫引入function,且前面不須加名字 * from BMI import * => 代表引入全部的function * Import function from the function library without adding a name in front of it * from BMI import * => represents the introduction of all functions ::: ## 回饋表單!Feedback Form! :::info [表單連結](https://forms.gle/Q1B4S49tHWFeTVZX7) :arrow_left: 請幫我們填寫表單!讓我們的程式教學更加進步~