# Python class on NTU ###### tags: `Python` `NTU` :::info Alt+P : Shell recall 上次指令 ::: --- ### - IDLE - > File -> New File<br> > Run -> Run Module<br> ### - Coding - * BIG-5 繁體中文 * UTF-8 Unicode > 1. Python 3.x default<br> > 2. Python 2.x 需要設定<br> > `# -*- coding: utf-8 -*-`<br> > Or<br> > `# coding: utf-8`<br> ![](https://i.imgur.com/7DcV9iO.png) ### - 資料型態 - * 數值型別: `int`, `float`, `bool`, `complex` > bool : True, False, None<br> > complex : 虛數<br> * 字串型別: `str` * 容器型別: `tuple`, `list`, `set`, `dict` > Container Type - <br> > 1. tuple : 序對 (元組) ; "( )" ; 有順序 ; 不可更改內容<br> > 2. list : 串列 (清單) ; "\[ \]" ; 有順序 ; 可修改內容<br> > 3. set : 集合 ; "{ }" ; 無順序 ; 可修改內容<br> > 4. dict : 字典 ; "{ }" ; Key-Value ; 可修改內容<br> ### - 函式 - **built-in function** 1. type() : 回傳變數型態 2. input() : 回傳一定是string 3. eval() : 字串轉數字 4. int() : 字串轉數字 5. range() : range(\[start\],stop,\[step\]) :::danger eval 危險語法,使用上要小心!! ::: :::info `range(\[start\],stop,\[step\])`<br> start: 起始值, default=0<br> stop: 停止條件<br> step: 迭代數字, default=1, 可以用負數<br> ::: --- :::info Python裡面所有東西都是object ::: :::info 宣告str的變數內容不可改變 ::: --- ### - 賦值 - ```python= x = 12 # ==> 把 12 賦值給 x ``` --- :::info * "in" 適用於: 字串、集合、清單、序對 * "in" 用在Dictionary, 只比對Key值, 非Value值 * Python以縮排來區分程式碼區塊, 縮排沒有對齊, 執行會出現錯誤 * 空白鍵與Tab鍵不同 ::: --- ### - 數字系統 - * Integer * Float * 方法: `bin()`, `oct()`, `hex()` ### - 字串系統 - * strip() : 去除左右兩側(頭尾)空白或是溢出字元('\n', '\t', ...etc) * lstrip() : 去除左側空白 * rstrip() : 去除右側空白 * capitalize() : 第一個字元變大寫 * title() : 每個單字的第一個字元變大寫 * swapcase() : 大小寫互換 * upper() : 全部大寫 * lower() : 全部小寫 * ==translate()== & ==maketrans()== : 字元轉換 * ==zfill(\<width\>)== : 字串補'0', 長度Width :::warning Only return bool (True/False) * endswith() * startswith() * istitle() * isupper() * islower() * isalnum() : 是否為\[a-z\], \[A-Z\], \[0-9\] * isalpha() * isdigit() : 純數字, 不接受小數點 * isspace() ::: * ==join()== : 字串融合 * split(\[sep=None, maxsplit=-1\]) : 以sep分割字串, 回傳==list== * count(sub\[, start\[, end\]\]) : 計算substring出現次數, 以==int回傳== * find(sub\[, start\[, end\]\]) : 回傳substring第一次出現的索引值, ==若不存在回傳<font color=FF0000>-1</font>== * index(sub\[, start\[, end\]\]) : 回傳substring第一次出現的索引值, ==若不存在回傳**ValueError**== * replace(old, new\[, count\]) : 代換 * splitlines(keepends) : 以"\\n"和"\\r"作為分割字元, 回傳list, keepends設成True, 會連同脫逸字元回傳 * ==partition(sep)== : 切割成3個tuple :::info 用法 1. str.upper(\<string\> 2. \<string\>.upper() PS: 字串方法不會改變字串內容 ::: ```python= # 練習 s3 = "Hello, world!" print(s3.zfill(17)) print('0' * 8 + s3) ``` --- :::info 物件.方法() ==> object.method() ::: --- ### List * ==sort()== ==> integer