# 大綱 - 基礎 Python **資料類型** (data type) - 整數:`integers` - 浮點數:`floats` - 布林:`booleans` - 什麼是 **物件** (object)? - 什麼是 **變數** (variable)? - 基本的 **運算子** (operator) - `arithmetic operators` - `integer division and modulus` - `comparison operators` - `Boolean operators` - `operator precedence` (introduced on next section) --- # 基礎 Python **資料類型** (data types) --- ## Types - 在程式裡的每一個 個體/實體 (entity) 都會有一個對應的類型 ![image](https://hackmd.io/_uploads/By_Z6KMup.png) --- ## Integer - ![image](https://hackmd.io/_uploads/S1bcatMO6.png) --- ## Float - ![image](https://hackmd.io/_uploads/SyrJx5GO6.png) --- ## Number Representation --- ### Float Representations - [人類算數採用十進制](https://zh.wikipedia.org/zh-tw/%E5%8D%81%E8%BF%9B%E5%88%B6) (decimal system): e.g. $1.234$ - 十進位是以**10**爲底數的數字系統,是在世界上應用最廣泛的進位制。 - 係利用 `10` 的冪 (powers of `10`) 的倍數來表示: $1 + {2 \over 10} + {3 \over 100} + {4 \over 1000}$ - 但浮點數有`有限小數`跟`無限小數`,不是所有實數都可以被有限的位數表示: e.g., $1/3$ - ${1 \over 3} = 0.333 = {3 \over 10} + {3 \over 100} + {3 \over 1000} + ...$ --> 電腦記憶體有限,無法紀錄無限的位數 --- ### Integer Representation - [二進制(英語:binary)在數學和數位電路中指以2為底數的記數系統,以2為基數代表系統是二進位制的。](https://zh.wikipedia.org/zh-tw/%E4%BA%8C%E8%BF%9B%E5%88%B6) - `0` or `1` - 在電腦裡的數字是用 `2` 的冪來表示;如一個二進位的數字:`'b1011`,對應的十進位數字為: - 1 x 2^0^ + 1 x 2^1^ + 0 x 2^0^ + 1 x 2^3^ = 1 + 2 + 0 + 8 = `11` = 1 x 10^0^ + 1 x 10^1^ --- ### Float Binary Representation - **浮點數** `二進制` 表示的原理與 `十進制` ㄧ樣,只是 **基數** 不同。如: - ${1 \over 2^1} + {0 \over 2^2} + {1 \over 2^3}$ = ${1 \over 2} + {0 \over 4} + {1 \over 8} = 0.5 + 0 + 0.125 = 0.625$ - `二進制` 依然有無法表示的無限小數,如 `0.1` - ![image](https://hackmd.io/_uploads/Skl0p6zOp.png) --- ### Float are not always exact ![image](https://hackmd.io/_uploads/rkF9R6z_p.png) --- #### :warning: be careful when comparing floats to one another ```python # Defining a positive infinite integer positive_infinity = float('inf') # Defining a negative infinite integer negative_infinity = float('-inf') print(positive_infinity + negative_infinity) ``` --> The result is `nan` --- ## Coding :information_source: Let's move to the `02 - Basic Data Types` juptyer notebook --- # **物件** (object) --- ## 物件是什麼? - Python 定義的實體 - 包含 `state` (data): 狀態 or 資料 - 包含 `method` (functionality): 功能 --> 封裝在 類別 (`class`) 中: **encapsulation** - 範例: 不同牌子,但同為「車子」的類別 ![image](https://hackmd.io/_uploads/BkO3jbQd6.png) --- ## :star: Everything in Python is an object - 每個物件有自己的屬性 (`attributes`) - 不同屬性分別用來表示 `state` 或 `functionality` --- ### 例如:`Integers` or `Floats` ![image](https://hackmd.io/_uploads/ryvoaWXdT.png) --- ![image](https://hackmd.io/_uploads/HJ93pZ7ua.png) --- ## 如何存取(一個)物件的屬性 - 利用 **dot notation(標記)**: `.` ![image](https://hackmd.io/_uploads/ryPc1f7up.png) - 利用 **call(呼叫)** 來執行表示功能的屬性: - 通常會需要提供額外的參數: ![image](https://hackmd.io/_uploads/BkatxG7da.png) --- ## Mutability ([可變性](https://terms.naer.edu.tw/detail/f1b7873c5e007f669f688cb02e2c8879/?seq=6)) and Immutability (Im-mutability [不可變](https://terms.naer.edu.tw/detail/d0119d4a1f4a2b87b94f97172a064faa/?seq=1)) 用來形容一個物件,端看他的狀態是否可以被改變;如: ![image](https://hackmd.io/_uploads/Bk0JMG7uT.png) --- ## Coding :information_source: Let's move to the `03 - Objects` juptyer notebook --- # Variables (「變」數) ## --> 替不同的物件命名 (name or label an object) :information_source: 在一個程式裡,時常會存在多個相同類別的物件;如:他開Toyota, 你開Benz... 等。 --- ## 如何命名一個物件? 利用 `assignment` ([賦值](https://terms.naer.edu.tw/search/?query_term=assignment&query_field=title&query_op=&match_type=phrase&filter_bool=and&filter_term='%E9%9B%BB%E5%AD%90%E8%A8%88%E7%AE%97%E6%A9%9F%E5%90%8D%E8%A9%9E'&filter_field=subcategory_1.raw&filter_op=term&tabaction=browse)) operator: `=` --> :warning: 不是指數學的"等於" 呦! - 而是將物件 **指定** 給一個變數 - 此時,變數就是該物件的一個 reference (參考):透過變數來存取該物件 - 如: ![image](https://hackmd.io/_uploads/H1XQIGmu6.png) --- ## Referenes 與 Variables 的關係 ![image](https://hackmd.io/_uploads/SJ588zmO6.png) --- ### 為什麼叫「變」數? - 不同執行時間,同一個變數可以用於參考不同的物件;如: ![image](https://hackmd.io/_uploads/BJnSDfXO6.png) - 亦可用於改變物件的狀態 (mutate) ![image](https://hackmd.io/_uploads/HyB5PzXdp.png) - 新增 '4' 到 `a` 指向的 list --- ## Assignment (賦值) 是如何發生的呢? - Python 會先求出 RHS 的值,再將該值「賦」 ![image](https://hackmd.io/_uploads/SJiSuzQ_T.png) - :::info LHS: Left-hand side RHS: Right-hand side ::: - 一般來說,`RHS` 會是一個比文字更複雜的表示式;如: ![image](https://hackmd.io/_uploads/BkH6cG7_p.png) --- ## 變數命名的方式 - case sensitive: 相同英文字母但大小寫不同,被視為"不同"的變數 - `必須` 遵從特定的規格:否則編譯器會回報錯誤 - `應該` 遵從特定的慣例:編譯器不一定會回報錯誤,主要用意是讓不同的人都能理解相同的程式 --- ### "必須" 遵從特定的規格 ![image](https://hackmd.io/_uploads/HyPGpzXdT.png) --- ### "應該" 遵從特定的慣例 ![image](https://hackmd.io/_uploads/SJyNaM7up.png) --- - PEP 8 Style Guide: https://www.python.org/dev/peps/pep-0008/ :thinking_face: 為什麼是 `8`? [PEP](https://peps.python.org/pep-0000/#introduction) 是 *Python Enhancement Proposal* 的縮寫;`PEP` 有多個主題,而主題是用後面的數字來索引。 --- - 慣例是拿來打破的,只要有好的原因且ㄧ致 - 在 PEP 8 Style Guide 裡提到: `A foolish consistency is the hobgoblin of little minds. (Emerson)` - *hobgoblin*: 妖怪 - ChatGPT: - "愚蠢的一致性是小心靈的小妖精。" - 不要 "墨守成規" > https://chat.openai.com/c/c9b538d3-006c-45bf-b28a-33ff0262d2a6 --- ## Coding :information_source: Let's move to the `04 - Variables` juptyer notebook --- # Arithmetic Operators --- ## Terminology (術語) - `Operator` (運算子):一種符號,表示運算時所執行的作業,例如`+,-,*,╱`等。 > https://terms.naer.edu.tw/detail/4e2bee0417cd8aba1d954e5d756b385b/ - 運算子的類型 - `arithmetic operators`: 算數運算子 - `comparison (or relational) operators`: 關連式運算子 - `logical operators`:邏輯運算子 --- - 用於運算子上的數個值稱為 `operands`: 運算**元** 依據運算元的數目,運算子可再分為: - `unary operator`: 一元運算子; 'u' 意指 "單一",指只有一個運算元的運算子,如代表負數的「-」號。 - `binary operator`: 二元運算子 - `ternary operator`: 三元運算子 - ... --- ## Arithmetic Operators ![image](https://hackmd.io/_uploads/BJ4fIYXOT.png) --- ## Operand Types - 運算子的類型決定運算子的運算內容 ![image](https://hackmd.io/_uploads/SkYELFQ_p.png) --- ## The **Power** Operator 某基數`冪`的操作,如: - 基數為**整數**搭配正或負的冪次 ![image](https://hackmd.io/_uploads/SygYDFmda.png) --- - 基數為**浮點數** ![image](https://hackmd.io/_uploads/r1XRdFQ_T.png) - Python 亦有支援[複數 (實部 + 虛部)](https://highscope.ch.ntu.edu.tw/wordpress/?p=15136) --- ## Python 如何實作算數運算子? - :thinking_face: 數字實際上就是物件 - 物件就會有 `狀態` 跟 `功能`,其中一個常用的功能就是 `__add__` 函式 - 如: ![image](https://hackmd.io/_uploads/Hy87iKQ_p.png) --- ### 任何一個類型都可以實作自己的 __add__ 函式 - 當我們使用 "+" 運算元時,Python 會呼叫該類型的 `__add__` 函式,如: ![image](https://hackmd.io/_uploads/ByWzV5XdT.png) --- ## Coding :information_source: Let's move to the `05 - Arithmetic Operator` juptyer notebook
{"title":"Python 3 Fundamentals 共學 2023 - Python Basics","description":"à some\tbasic\tPython\ttypesà integers à floats à booleansà a\tbrief\texplanation\tof\twhat\tobjects areà basic\toperatorsà arithmetic\toperatorsà integer\tdivision\tand\tmodulusà comparison\toperatorsà Boolean\toperatorsà operator\tprecedence","contributors":"[{\"id\":\"1fff2ed2-c6f3-4898-8309-30a56b3d0f05\",\"add\":7986,\"del\":2183}]"}
    390 views