# Python 特色 ###### tags: `Python` * 優雅、明確、簡單 * Glue language: with C、C++ or Java * Libraries: * Numpy 科學計算 * Twisted 網路引擎 * Scrapy 數據挖掘與統計 * Pillow 圖形處理 * Object-Oriented programming * Errors * Syntactic Errors 語法錯誤 * Semantic Errors 語意錯誤 or Logic Errors 邏輯錯誤 * Run-Time Errors 執行時期錯誤 * Python翻譯器 * 編譯器 Source code -> Bytecode * Lexical Analysis 字彙分析 * Parsing 語法分析 * Semantic Analysis 語意分析 * 虛擬機器 (PVM: Python Virtual Machine) Bytecode -> Machine Code * 資料型別 * 數值資料型別 Numeric Type: int, float * 文字序列資料型別 Text Sequence Type: str * 序列資料型別 Sequence Type: list, tuple, range * 映射資料型別 Mapping Type: dict * 物件 Object * 屬性 Attribute / Property * 方法 Method * 類似物件可以歸類成類別 Class * 結構化程式設計 * 循序結構 * 選擇結構 * 重複結構 * 保留字 Reserved Word (Keyword) :::info and, as, assert, async, await, break, class, continue, def, del, elif, else, except, False, finally, for, from, global, if, import, in, is, lambda, None, nonlocal, not, or, pass, raise, True, return, try, while, with, yield ::: * float 類別 * float() 函式 * round() 函式 * is_integer() 方法: float.is_integer() * decimal 模組 ``` import decimal # decimal.Decimal() 方法 # decimal.Decimal.from_float() 函式 # decimal.getcontext() 函式 # 屬性 prec 設定有效位數, 預設28 # 屬性 rounding 設定進位方式, 預設 ROUND_HALF_EVEN # ROUND_HALF_EVEN: 四捨六入, 數值為五時, 前面是奇數就進位, 偶數就捨去 # ROUND_DOWN: 無條件捨去 # ROUND_UP: 無條件進位 ``` * 運算子分類 * 一元運算子 Unary Operator * 二元運算子 Binary Operator * 運算子種類 * 指定運算子 Assignment Operator: ***'\='*** * 算術運算子 Arithmetic Operator: ***'\+', '\-', '\*', '\/', '\//', '\%', '\*\*'*** * 複合指定運算子 Shorthand Assignment Operator: ***'\+=', '\-=', ...*** * 關係運算子 Relational Operator: ***'\==', '\!=', ...*** * 邏輯運算子 Logical Operator: ***'and', 'or', 'not'*** * 位元運算子 Bitwise Operator: ***'\& (And)', '\| (Or)', '\^ (Xor)', '\~ (Not)'*** * 移位運算子 Shift Operator: ***'\>>', '\<<'*** * 成員運算子 Membership Operator: ***'in', 'not in'*** * 身分運算子 Identity Operator: ***'is', 'not is'*** --- id(): 取得物件使用的記憶體位址 float([integer])函式: 整數轉浮點數 int([float])函式: 浮點數轉整數 round([float])函式: 浮點數轉整數 str([value])函式: 數值轉字串 bool([data])函式: 轉布林值 int([string])函式: 整數字串轉整數 float([string])函式: 浮點數字串轉浮點數 eval([string])函式: 字串轉數值 str.format()方法 - 引數概念 --- * 例外處理 ```python= try: #monitor code except ZeroDivisionError as e: print(e) except ValueError as e: print(e) except NameError as e: print(e) except IndexError as e: print(e) except IOError as e: print(e) except FileNotFoundError as e: print(e) except Exception as e: print(e) finally: #Code ```