## week03_0921_variable expression ### {上週使用函數} 1. print() - 輸出內容 如果要放置變數到輸出:print("" % () ) - sep:分隔、end:結束 2. input() - 輸入內容 ""放顯示出來的文字 3. type() - 顯示資料型態 4. int() - 整數 5. float() - 浮點數 6. str() - 字串 ### [inclass practice] ### {概念練習} 1. 變數命名 - o:只能由大小寫英文字母、數字、底線、中文組成 - x:第一個字母不能是數字、不能使用python內建保留字 2. 變數資料型態 - 數值型態:int、float、bool(True、False,通常用在條件運算) - 字串型態:str 3. 數值型資料類型有哪些 - 有int、float、bool 4. " + " 運篹子 - for數值運算、字串組合 - 資料型態相同才能執行 5. 輸出: " %d %s " % (參數列) - %d:整數 - %s字串 6. 輸出: " {0} {1} ".format(參數列) - 變數的位置在字串裡用大括弧 - {0}、{1}指定format後面變數的順序 7. 輸入命令 - type:顯示項目資料型態 8. 運算子優先順序 1. () 2. ** 3. +、-(正數、負數) 4. *、/、%、// 5. +、-(加法、減法) 6. ==、!=、>、<、>=、<= 7. not、and、or 8. =、+=、-=、*=、/=、%=、//=、**= 9. view->Toggle Line Numbers可以顯示出每行號碼 10. 多行註解用三個單引號或雙引號打在段落的最外面 ### {綜合演練} ### 實作題3 ``` 矩形的面積是長*寬、周邊長是(長+寬)*2 請設計程式讓使用者輸入矩形的長和寬,然後計算矩形面積和周邊長。 ``` 1. input - 長 - 寬 2. process - 矩行的面積是長*寬 - 周邊長是(長+寬)*2 3. output - 面積 - 周邊長 ```python length = 5 width = 6 area = length * width perimeter = (length + width) * 2 print("面積:", area) print("周長:", perimeter) ``` 面積: 30 周長: 22 ```python length = float(input("enter length:")) width = float(input("enter width:")) area = length * width perimeter = (length + width) * 2 # print(type(length)) print("面積:", area) print("周長:", perimeter) ``` enter length:6 enter width:5 面積: 30.0 周長: 22.0 ```python area = 40.0 perimeter = 100 print("面積:", area, "周長:", perimeter) output = "面積:" + str(area) + "周長:" + str(perimeter) print(output) ``` 面積: 40.0 周長: 100 面積:40.0周長:100 ```python print("面積: %10.1f, \n周長: %d" %(area, perimeter) ) # 移一個位子\t,換行\n print("面積: {1:10.1f}, \n周長: {0}".format(perimeter, area) ) # 變數的位置在字串裡用大括弧 # 用{0}、{1}指定format後面變數的順序 ``` 面積: 40.0, 周長: 100 面積: 40.0, 周長: 100 ### 實作題4 ``` 許多人出國時會有公制轉英制的困擾,以長度為例,英制和公制的長度轉換公式為: 1 inch (英吋) = 2.54 cm (公分) 請設計程式讓使用者輸入公制的身高(cm),然後計算出英制的高度是幾英呎、幾英吋 1 英呎 = 12 英吋 ``` 1. input - 身高 2. process - cm to inch (/2.54) - inch to feet (/12) 3. output - 幾英尺 - 幾英寸 ```python height = float(input("輸入身高:")) to_inch = height / 2.54 to_feet = to_inch / 12 print("你的身高為多少英吋: {0:.1f}, \n你的身高為多少英尺: {1:.1f}".format(to_inch, to_feet)) ``` 輸入身高:163 你的身高為多少英吋: 64.2, 你的身高為多少英尺: 5.3 ### [afterclass practice] 1. 綜合演練 實作題1-2 (需抄題在markdown cell) 2. 教學影音 lesson 5[、6、1] ### 實作題1 ``` 1. 一年一班只有2位同學,設計程式讓老師分別輸入2位同學的姓名及成績,然後計算成績總分,最後以下圖格式列印。 ``` ```python name1 = input("請輸入第一位學生的姓名:") score1 = int(input("請輸入第一位學生的成績: ")) name2 = input("請輸入第二位學生的姓名:") score2 = int(input("請輸入第二位學生的成績: ")) total = score1 + score2 print() print("姓名 成績") print("%-4s %3d" % (name1, score1)) print("%-4s %3d" % (name2, score2)) print("成績總分為:%3d" % (total)) ``` 請輸入第一位學生的姓名:林大毛 請輸入第一位學生的成績: 78 請輸入第二位學生的姓名:陳學文 請輸入第二位學生的成績: 100 姓名 成績 林大毛 78 陳學文 100 成績總分為:178 ### 實作題2 ``` 2. 計程車計費方式第1公里為85元,多1公里加收20元。設計程式讓運將輸入乘客的搭乘公里數,然後計算乘車費用。 ``` ```python km = int(input("請輸入路程公里數 (整數):")) money = 85 + (km - 1) * 20 print("你的車程車資費為:" + str(money)) ``` 請輸入路程公里數 (整數):8 你的車程車資費為:225 ### [self practice] 運算子先後優先順序 ```python # 1 a = 5 + 3 * 2 / (7 - 4) print(a) ``` 7.0 ```python # 2 b = (10 % 3)**2 + 4 // 2 print(b) ``` 3 ```python # 3 c = (8 + 2) * (5 - 3) / 2 print(c) ``` 10.0 ```python # 4 d = 2 ** 3 + 4 ** (1 / 2) - 1 print(d) ``` 9.0 ```python # 5 e = (10 > 5)or(3 != 3)and(8 // 2 <= 4) print(e) ``` True ```python # 6 f = "Hello" + " " + "World" * 2 print(f) ``` Hello WorldWorld ```python # 7 g = (15 / 3) % 2 != 0 and not (10 > 7) print(g) ``` False ```python # 8 h = (3 + 2)**2 / 4 * 5 % 6 print(h) ``` 1.25 ```python # 9 i = 2 * (4 + 6) - (5 % 3)**2 print(i) ``` 16 ```python # 10 j = len("Python") + int("3") + (5 > 2) print(j) ``` 10