## 2021/03/10(三) RAP 第2堂課補充: ### 觀念釐清 1. 變數命名:需易於辨識的字詞,目的是為了讓以後開發時不會造成混淆,所以盡量不要單一字母或無意義的單詞。 2. list[ ]是串列/清單函數,係指存資料的容器,並非字串,字串通常是以””表示。 3. [return v.s. print差別](https://www.itread01.com/content/1550151497.html) * return:1.給變量賦值、2.當遇到return函數時就會結束,常用於無限循環迴圈。 * print:1.印出執行結果、2.當遇到print函數時仍繼續執行下一段程式。 ### 補充函式 1. len(str)獲取資料長度 - [len(str)](https://www.itread01.com/p/450915.html) -str指要計算的字串、列表、字典、元組等。 ```python= data = [1,3,5,7,9,11] print len(data) ``` ## 參考資源: ### 迴圈 Loop - 天下創新學院-Python程式設計輕鬆學-第13講:[for-loop、range、zip](https://colab.research.google.com/drive/1Mx8sWJ-73x_rRADqYzMXyehmk1fuu64m?usp=sharing) - 天下創新學院-Python程式設計輕鬆學-第14講:[while-loop、break、continue、pass](https://colab.research.google.com/drive/1ca3iWIdr9mq4e3etdNkHD_i6QpXXWY5q?usp=sharing#scrollTo=xvURzBGg--rW) - [迴圈幫我們一次解決重複的事](https://medium.com/ccclub/ccclub-python-for-beginners-tutorial-4990a5757aa6) - [搞懂5個Python迴圈常見用](https://www.learncodewithmike.com/2019/12/python.html) ### 函式 Fuction - 天下創新學院-Python程式設計輕鬆學-第16講 - [利用函式處理單一的特定功能](https://medium.com/ccclub/ccclub-python-for-beginners-tutorial-244862d98c18) - [5個必知的Python Function觀念整理](https://www.learncodewithmike.com/2019/12/python-function.html) ### 上課範例 1. 九九乘法表 - for loop ```python= for i in range(2, 10): for j in range(1, 10): print(f'{i} * {j} = {i*j}') print('\n') ``` - while loop ```python= i = 2 while i <= 9: j = 1 while j <=9: print(f'{i} * {j} = {i*j}') j = j + 1 i = i + 1 ``` 2. 七九乘法表 - for loop + break ```python= for i in range(2, 10): for j in range(1, 10): print(f'{i} * {j} = {i*j}') print('\n') if i == 7: break ``` - while loop + break ```python= i = 2 while i <= 9: j = 1 while j <=9: print(f'{i} * {j} = {i*j}') j = j + 1 if i == 7: break i = i + 1 ``` 3. BMI計算器 - 單一計算 ```python= def bmi_calculator_1(weight, height): """ BMI = 體重/身高**2 weight: int, 體重(以公斤計) height: float, 身高(以公尺計) """ bmi_value = weight / height**2 return bmi_value ``` - 串列計算 ```python= def bmi_cal_list(w_list, h_list): bmi_output=[] for w, h in zip(w_list, h_list): bmi = round((w/(h/100)**2), 2) bmi_output.append(bmi) return bmi_output ``` ## 問答區: -