--- slideOptions: theme: serif --- # 變數與 I/O ### Python Day 1 ###### 08.20 鄭余玄 ---- ### 來寫第一支程式吧! - 半形英文輸入 ![](https://i.imgur.com/w0HL1ro.png) Note: 1. 提醒 ' 在 Enter 旁邊 1. Shift + Enter 執行 1. 或是 Ctrl + Enter 執行 1. 帶一下 Jupyter Notebook 使用 ---- ### `print` - 輸出內容並且換行 ```python print('Hello, world') print(1 + 1) ``` ---- ### `print` - `print(A, B, C)` 輸出以空白分隔,換行結尾 ```python print('Hello, world') print('1 + 1 =', 1 + 1) ``` Note: - 確認所有人都能使用 --- ### 常見資料型態 (Type) - `str` 字串 string - `int` 整數 integer - `float` 小數、浮點數 Note: - 資料通常叫做物件 (object) - 物件占有空間 ---- ### 範例 ```python print(-2, -1, 0, 1) print('This is string') print("This is another string") print(3.14) ``` Note: - 字串可以用單或雙引號 ---- ### 用 `type` 看型態 ```python print(type(-2), type(0)) print(type('This is string')) print(type("This is another string")) print(type(3.14)) ``` ---- ### 數字計算 - `#` 井字號後是註解 - `2**10` 表示 $2^{10}$ ```python print(8 + 9) print(1 - 378) print(53 * 179) # 乘法是用 * 符號 print(11 / 3) # 除法 print(11 // 3) # 整除 (無條件捨去) print(11 % 3) # 取餘數 (ex: 11 mod 3) print(2 ** 10) # 次方 (ex: 2 的 10 次方) ``` Note: 1. 乘法、除法只能是 `*` 和 `/` - 乘法 * 是 Shift + 8 - 除法是倒斜線 / 在 RShift 旁 - 模 % 是 Shift + 5 - 註解 # 是 Shift + 3 ---- ### 更多運算 - `()` 小括號結合運算 ```python print(1 + 2 + 3 * 4) print(1 + (2 + 3) * 4) print((1 + (2 + 3)) * 4) print(2 ** 3 ** 2) print((2 ** 3) ** 2) ``` Note: - 不能用中括號、大括號 ---- ### 錯誤:SyntaxError - 語法錯誤 ![](https://i.imgur.com/LDCxrEJ.png) --- ## 變數 - 變數名稱 = 資料物件 ```python name = 'Scott' height = 180 pi = 3.14 print('My name is', name) print('My height is', height) ``` Note: - 與 C 概念不同 - 避免底線開頭 - (不能是數字開頭) ---- ### 變數運算 ```python height = 180 width = 0.5 pi = 3.14 vol = pi * (width ** 2) * (height / 100) print('My volume is', vol, 'm^3') ``` ---- ### 也有各種你知道的函數 ```python a, b = -5, 3 print(abs(a)) # 絕對值 absolute value print(max(a, b)) # 最大值 maximum print(min(a, b)) # 最小值 minimum ``` Note: - 初始化 - 多個 max, min ---- ### 錯誤:NameError - 未定義的變數 ```ipython >>> n # 未定義的變數 Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'n' is not defined ``` ---- ### 練習:交換變數 ```python a = 123 b = 456 print(a, b) # 123 456 # do something print(a, b) # 456 123 ``` Note: - 暫存變數 ```python a = 123 b = 456 print(a, b) # 123 456 t = a # (a, b, t) = (123, 456, 123) a = b # (a, b, t) = (456, 456, 123) b = t # (a, b, t) = (456, 123, 123) print(a, b) # 456 123 ``` - 直接交換 ```python a = 123 b = 456 print(a, b) # 123 456 b, a = a, b print(a, b) # 456 123 ``` ---- ### **錯誤**示範 ```python a = 123 b = 456 print(a, b) # 123 456 a = b # (a, b) = (456, 456) b = a # (a, b) = (456, 456) print(a, b) # 456 456 ``` --- ### 字串操作 ```python print('Py' + 'thon') print('P' + 'y' * 10 + 'thon') ``` ---- ### 範例 ```python start = 'Py' end = 'thon' print(start + end) print('P' + 'y' * 10 + end) ``` ---- ### 這個是? ```python print('123' + '1') print('6' * 3) ```` ---- ### 錯誤:TypeError - 運算要注意型態 ![](https://i.imgur.com/yfQjmXe.png) Note: - 學會看錯誤訊息 ---- ### 轉換型態 - 使用 `int()`, `str()`, `float()` 轉型 ```python a = 123 b = '456' print(a + int(b)) print(str(a) + b) ``` Note: - 轉換型態 簡稱 轉型 ---- ### ~~一言不合就轉隊~~ ![](https://i.imgur.com/SdGn6Hh.png) ---- ### 「Error 都是暫時的, ### 沒有解不掉的 Bug, ### 莫忘心靈祥和。」 ###### —師父開示 ---- ## 更多型態 - `int`、`float`、`bool`、`complex` - `str`、`list`、`tuple`、`dict` --- ## I/O #### Input/Output ---- ### `input` - 等待輸入,並以 `str` 型態存入變數中 ![](https://i.imgur.com/rMu1pko.png) ![](https://i.imgur.com/LyABIAW.png) ---- ### `input` 提示 ![](https://i.imgur.com/pIqi4LQ.png) ![](https://i.imgur.com/5wIiu9Y.png) ---- ### `help` - `help(print)` ![](https://i.imgur.com/lFTaNsp.png) Note: - `print?` - `print??` ---- ### 練習:算成績 - 期末考快 GG 了,用 Python 幫自己算算成績吧! - 輸入一次期中考成績、一次期末考成績,和兩次作業成績 - 輸出總成績 - 期中 * 0.3 + 期末 * 0.4 + 作業平均 * 0.3 ---- ### 示意圖 ![](https://i.imgur.com/GtvOJww.png) Note: - 10610CS 135600 計算機程式設計二 --- ## Q & A ##### ~~疑難雜症~~ ---- ### 錯誤:ZeroDivisionError - 檢查除數的變數(`b`)是否為零 ```ipython >>> a, b = 1, 0 >>> a / b Traceback (most recent call last): File "<stdin>", line 1, in <module> ZeroDivisionError: division by zero ``` ---- ### 有沒有 `print` 差在哪? - `print` 是 ==Python== 規定的輸出方式 - 不加 `print` 是 ==Jupyter== IPython 的功能 - 輸出可能與 `print` 結果**不同** ![](https://i.imgur.com/ysDEz37.png) ---- ### 奇妙的註解用法(? ![](https://i.imgur.com/PFqdm1a.png) <style> .slide-number{ margin-bottom:10px !important; width:100%; text-align:center; font-size:25px !important; background-color:transparent !important; color:black !important } .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hljs-comment, .hljs-quote { color: #998; font-style: italic; } .hljs-keyword, .hljs-selector-tag, .hljs-subst { color: #333; font-weight: bold; } .hljs-number, .hljs-literal, .hljs-variable, .hljs-template-variable, .hljs-tag .hljs-attr { color: #008080; } .hljs-string, .hljs-doctag { color: #d14; } .hljs-title, .hljs-section, .hljs-selector-id { color: #900; font-weight: bold; } .hljs-subst { font-weight: normal; } .hljs-type, .hljs-class .hljs-title { color: #458; font-weight: bold; } .hljs-tag, .hljs-name, .hljs-attribute { color: #000080; font-weight: normal; } .hljs-regexp, .hljs-link { color: #009926; } .hljs-symbol, .hljs-bullet { color: #990073; } .hljs-built_in, .hljs-builtin-name { color: #0086b3; } .hljs-meta { color: #999; font-weight: bold; } .hljs-deletion { background: #fdd; } .hljs-addition { background: #dfd; } .hljs-emphasis { font-style: italic; } .hljs-strong { font-weight: bold; } </style>