# Python basics 講師:極速車神大佬 --- ## Outline - 輸出 - 註解 - 變數 - 資料型態 - 運算子 - 輸入 - 有料的 --- ## 輸出 Output 可以幫我們列印出文字 ! ---- ```python print('Hello World') ``` 可以直接放到 VScode 試試看 ---- ```python print('Hello World') ``` - `print('Hello World')` 是一行程式碼 - `print()` 是能夠輸出文字的東西 ( 函數 ) - 一串文字要用兩個 `'` 框起來 ---- ```python print('Morning') print('Afternoon') print('Evening') ``` - 程式由上而下執行 - `print()` 每次都會換行 - 因此輸出 ```txt Morning Afternoon Evening ``` ---- Q: 如果不加引號會怎樣 ```python print(Morning) ``` ---- - 直譯器不會把這串 `Morning` 當作是一串文字 - 你會看到編輯器上面的顏色不一樣 ```python print(Morning) ``` ---- 我想要印出多行字串 以下不是個好方法,過於冗長 ```python print('The purple elephant danced through the spaghetti') print('rain while whispering secrets to the bewildered doorknob.') print('Meanwhile, a clock with no hands argued with a loaf of') print('bread about the philosophy of time, as a banana rode a') print('unicycle across the ceiling.') print('Somewhere in the distance, a choir of invisible ducks') print('quacked in perfect harmony, celebrating the grand') print('opening of an underwater firework shop.') print('The air smelled like forgotten dreams and misplaced socks,') print('swirling together in a symphony of nonsense') ``` ---- 這是一個好方法 ```python print('''The purple elephant danced through the spaghetti rain while whispering secrets to the bewildered doorknob. Meanwhile, a clock with no hands argued with a loaf of bread about the philosophy of time, as a banana rode a unicycle across the ceiling. Somewhere in the distance, a choir of invisible ducks quacked in perfect harmony, celebrating the grand opening of an underwater firework shop. The air smelled like forgotten dreams and misplaced socks, swirling together in a symphony of nonsense.''') ``` ---- 小知識:在 Python 裡面,引號可以用 `'` 或者 `"` ---- `print()` 也可以直接列印數字,這邊就不用加引號 以下兩者是一樣的結果 ```python print(3) print('3') ``` ---- `print()` 裡面可以用逗點隔開,預設會用一個空格隔開 ```python print(3, 'q', 'very', 'much') ``` 輸出: ```txt 3 q very much ``` ---- 可以用 `sep=` 來改用不同字串隔開 ```python print(3, 'q', 'very', 'much', sep=',') ``` 輸出: ```txt 3,q,very,much ``` ---- 也可以用 `end=` 來控制句尾字元 ( 預設是換行 ) ```python print('3 + 2 =', end = ' ') print(5) ``` 輸出: ```txt 3 + 2 = 5 ``` ---- 但如果我想要印出引號 ```python print(''這個是引號') ``` 會壞掉,因為直譯器認為在第二個引號這串文字就已經結束 後面的東西他不認得 ---- ### 跳脫字元 有一些字元直接放在引號裡面跟原本的用途相撞,所以會用到跳脫字元 會先打一個反斜線 ( 反白鍵下面 ) 後面接你要印出的特殊字元 - `\'` 印出 `'` - `\"` 印出 `"` - `\\` 印出 `\` - `\n` 會換行 還有很多在這份 [跳脫字元表](https://docs.python.org/3/reference/lexical_analysis.html#escape-sequences) ---- ### 練習 [SPROUTOJ 663](https://tioj.sprout.tw/contests/4/problems/663) --- ## 註解 有點像在程式碼中做筆記 提醒寫程式的人知道現在在做什麼 ---- ### 單行註解 用 `#` 開始就代表後面的東西是單行註解 ```python # 這是單行註解 # 以下會輸出 Hello World print('Hello World') ``` ---- ### 多行註解 多行註解會用 `'''` 或者 `"""` 來框起來 ( 跟多行字串類似 ) ```python # 以下是多行註解的用法 ( 單引號版本 ) ''' 這裡是多行註解 ''' # 以下是多行註解的用法 ( 雙引號版本 ) """ 這裡也是多行註解 """ ``` --- ## 變數 有點像是數學的未知數... ? ---- ```python name = 'Lucas' ``` 這樣 `name` 就是一個代表 `'Lucas'` 的變數 ---- ```python name = 'Lucas' print('My name is', name) ``` 因為 `name` 代表 `'Lucas'` 因此這裡會輸出 ```txt My name is Lucas ``` ---- ```python x = 10 y = 20 print(x, y) # 10 20 ``` **可以注意到 `=` 其實就是「賦值」的概念** - 第一行讓 `x` 賦值為 `10` - 第二行讓 `y` 賦值為 `20` **跟數學的「相等」有很大的差異** ---- 一些比較特別的賦值小技巧 ```python a, b = 10, 20 # 讓 a 設成 10, b 設成 20 a, b = b, a # 交換 a, b ``` ---- 變數命名規則 - 不能用保留字 ( 如 if, for, while 等等會在編輯器變色的字 ) - 可以用數字、字母、底線命名,但數字不能放在開頭 - 盡量取有意義的變數名字 ( 不要取 `a`, `aa`, `aaa` 這種東西 ) ---- 不好的範例 ```python a = 'Lucas' aa = 19 print('My name is', a, 'and I am', aa, 'years old.') ``` 好的範例 ```python name = 'Lucas' age = 19 print('My name is', name, 'and I am', age, 'years old.') ``` ---- 有關於 Python 變數的運作方式可以看看 [這個影片](https://youtu.be/0Om2gYU6clE?si=-2hiqCx8ZE3aNHfh) --- ## 資料型態 變數的種類...? ---- 常見基礎資料型態 - int 整數 - float 浮點數 (小數) - bool 布林值 - str 字串 ---- ### int 整數 ```python a1 = 10 a2 = 20 a3 = 30 s3 = a1 + a2 + a3 # 60 ``` ---- ### float 浮點數 ```python f1 = 5.5 f2 = 4.3 g2 = f1 + f2 # 9.8 ``` ---- ```python f1 = 5.5 f2 = 4.3 f3 = 3.99 g3 = f1 + f2 + f3 # 13.790000000000001 (?) ``` 這個稱作浮點數誤差 ---- ![image](https://hackmd.io/_uploads/rySx5Egtyg.png) ---- ### bool 布林值 只有兩種值:`True` 或 `False` 通常會跟其他條件判斷 `if`, `for` 等等一起用 ( 之後會教到 ) ```python a = True print(a) ``` ---- ### string 字串 `print('Hello World')` 就是印出一個字串 ! ```python s1 = 'Hello World' s2 = '''Hello World, Good morning.''' ``` ---- 看資料型態的方法 ```python a1 = 1 a2 = 1.0 a3 = True a4 = '1.0' # 輸出:<class 'int'> <class 'float'> <class 'bool'> <class 'str'> print(type(a1), type(a2), type(a3), type(a4)) ``` ---- 資料型態轉型 ```python int(3.5) # (浮點數轉整數) 無條件捨去 float(3) # (整數轉浮點數) 加上 .0 變成 3.0 str(3.5) # (浮點數轉字串) 變成 '3.5' bool(3.2) # (浮點數轉布林值) 非 0 變成 True, 其他變成 False ``` ---- 還有一些進階的資料型態,之後會教 list dict set ... --- ## 運算子 ---- 國小數學: ```python print(11 + 3) print(11 - 3) print(11 * 3) print(11 / 3) ``` ---- 比較特別的數學 ```python print(11 // 3) # 整數除法 ( 取除法的商 ) print(11 % 3) # 取餘數 print(11 ** 3) # 11 的 3 次方 ``` ---- 運用在賦值上 ```python a = 5 a = a + 3 # 8 b = a + 4 # 9 print(a, b) ``` ---- 簡寫 ? ```python a = 5 a += 3 # 相等於 a = a + 3 print(a) # 8 ``` ---- 字串也有一些操作 ```python string = 'Hello' print(string * 3) # HelloHelloHello ( 重複3次 ) print(string + ' World') # Hello World ( 字串連接 ) ``` ---- ### f-string 在引號前面加上 `f`,在引號裡面可以用 `{}` 放表達式 這樣就不用一直逗號 ```python print(f'3 + 2 = {3+2}') ``` --- ## 輸入 ---- 不是每個人都叫做 Lucas ```python name = 'Lucas' print('My name is', name) ``` ---- 透過 `input()` 我們可以在 terminal 打字進去 按 `enter` 就可以完成 `input()` `name` 就會變成我們打的名字 ```python name = input() print('My name is', name) ``` ---- `input()` 裡面的字串會被印出來 ```python name = input('What\'s your name? ') # 跳脫字元 print('My name is', name) ``` ![image](https://hackmd.io/_uploads/H13Knj-Yyg.png) ---- :::warning `input()` 輸入的是字串型態的變數 ::: ```python age = input('How old are you? ') print('I am', age, 'years old.') print(type(age)) # str ``` ---- ### 小練習 [SPROUTOJ 668](https://tioj.sprout.tw/contests/4/problems/668) --- ## 有料的 [真的有料](https://github.com/satwikkansal/wtfpython) --- ## 作業練習 [SPROUTOJ 663](https://tioj.sprout.tw/contests/4/problems/663) [SPROUTOJ 666](https://tioj.sprout.tw/contests/4/problems/666) [SPROUTOJ 667](https://tioj.sprout.tw/contests/4/problems/667) [SPROUTOJ 668](https://tioj.sprout.tw/contests/4/problems/668)
{"title":"Python basics","description":"講師:極速車神大佬","contributors":"[{\"id\":\"6a375517-4167-4b7c-a983-1e595a29262c\",\"add\":7239,\"del\":882}]"}
    208 views