# 資研 10/6 社課講義 [**提問表單**](https://forms.gle/z4dPC1JPV6uA2q9w7) [**上課PPT**](https://www.canva.com/design/DAFwXetpxgU/pbchSwlIVgiojxGHGU06sA/view?utm_content=DAFwXetpxgU&utm_campaign=designshare&utm_medium=link&utm_source=publishsharelink) --- ## 輸入 Input :::warning **變數 Variable** > 可以用`=`賦予值 - 命名規則 1. 開頭只能是<font color="#CE0000">**字母**</font>或<font color="#CE0000">**底線**</font> 2. 名稱只能包含<font color="#CE0000">**數字**</font>、<font color="#CE0000">**字母**</font>、<font color="#CE0000">**底線**</font> 3. 大小寫代表不同變數 4. 不可和[**保留字**](https://www.w3schools.com/python/python_ref_keywords.asp)相同 5. 輸出時不須加上`''` 6. `f-string:f'字串{變數}'` ::: - 函式:`input()` - 語法:`變數 = input()` - 範例 ```python= # 變數名稱-字母 a = 12 print(a) # 輸出:12 # 變數名稱-底線 _hi = '你好' print(_hi) # 輸出:你好 # 輸出-f-string b = input() print(f'你輸出了{b}') # 輸出:你輸出了(你輸入的東東) # 輸入 x = input() print(x) # 輸出:(你輸入的東東) # 輸入-提示字 y = input('請輸入:') print(y) # 輸出:請輸入:(你輸出的東東) ``` ## 註解 Comment - 快捷鍵:`Ctrl`+`/` - 使用時機:解釋程式碼用途、測試 ```python= print() # 註解前 # print() # 註解後 ``` ## 資料型態 Data Types | Text | Numeric | boolean | |:----:|:-------:|:------:| | `string` | `int`、`float` | `bool` | - 容器 | Sequence | Mapping | Set | |:--------:|:-------:|:----:| | `list`、`tuple` | `dict` | `set` | - 函式:`type()` - 用途:檢查資料型態 ```python= # 字串 a = 's' print(type(a)) # 輸出:<class 'str'> # 整數 b = 1 print(type(b)) # 輸出:<class 'int'> # 小數 c = 1.25 print(type(c)) # 輸出:<class 'float'> # 布林值 d = True print(type(d)) # 輸出:<class 'bool'> ``` ## 數字 Numbers - 種類:`int`、`float` - 可使用`int()`、`float()`**轉換** - <font color="#CE0000">**輸入資料時若需加以運算需要先轉成**</font>`int` - 可進行運算 :::warning **運算子 Operators** 1. **算數運算子** | + | - | * | ** | / | // | % | |:----:|:----:|:----:|:---:|:---:|:---:|:---:| | 加 | 減 | 乘 | n次方 | 除 | 整除 | 取餘數 | 2. **邏輯運算子** 3. **比較運算子** 4. **身分運算子** 5. **關係運算子** 6. **賦值運算子** 7. 位元運算子 ```python= # + A = 5 + 5 print(A) # 輸出:10 # - B = 5 - 2 print(B) # 輸出:3 # * C = 2 * 5 print(C) # 輸出:10 # ** D = 2 ** 3 print(D) # 輸出:8 # / E = 11 / 2 print(E) print(type(E)) # 輸出:5.5 # <class 'float'> # // F = 11 // 2 print(F) print(type(F)) # 輸出:5 # <class 'int'> # % G = 11 % 2 print(G) print(type(G)) # 輸出:1 # <class 'int'> # 結合輸入 x = int(input()) y = int(input()) print(x+y) ``` ::: ## 練習題 <font color="#CE0000">a002因為網站主要是給C++的使用者,所以a002在輸入上對新手比較困難,大家可以先用我改過的範例測資看看有沒有成功就好,有興趣的可以再去挑戰</font> **Zerojudge** [**連結**](https://zerojudge.tw/) >注意事項:zerojudge內的輸入不可使用<font color="#CE0000">提示字</font> a002.範例測資 >2 >3 >輸出:5 :::spoiler d483.範例程式碼 ```python= print('hello, world') ``` ::: :::spoiler a001.範例程式碼 ```python= a = input() print('hello,', a) ``` ::: :::spoiler a002.範例程式碼-修改後 ```python= a = int(input()) b = int(input()) print(a+b) ``` ::: :::spoiler a002.範例程式碼-原版 ```python= # 把整串都讀進來(包含space) a = input() # 開一個空白列表 L = [] # 用迭代迴圈分析是不是space for i in a.split(' '): # 如果不是就加進列表 if i != '': L.append(i) # 總和 sum = 0 # 把列表裡的數字一一相加 for i in L: sum += int(i) # 輸出 print(sum) ``` ::: --- ## 補充資料 **推薦網站:**[**W3Schools**](https://www.w3schools.com/)