大綱

  • 基礎 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

Integer

  • image

Float

  • image

Number Representation


Float Representations

  • 人類算數採用十進制 (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


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

Float are not always exact

image


Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
be careful when comparing floats to one another

# 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

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Let's move to the 02 - Basic Data Types juptyer notebook


物件 (object)


物件是什麼?

  • Python 定義的實體
    • 包含 state (data): 狀態 or 資料
    • 包含 method (functionality): 功能
      > 封裝在 類別 (class) 中: encapsulation
  • 範例: 不同牌子,但同為「車子」的類別
    image

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Everything in Python is an object

  • 每個物件有自己的屬性 (attributes)
    • 不同屬性分別用來表示 statefunctionality

例如:Integers or Floats

image


image


如何存取(一個)物件的屬性

  • 利用 dot notation(標記): .

    image

  • 利用 call(呼叫) 來執行表示功能的屬性:

    • 通常會需要提供額外的參數:
      image

Mutability (可變性) and Immutability (Im-mutability 不可變)

用來形容一個物件,端看他的狀態是否可以被改變;如:
image


Coding

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Let's move to the 03 - Objects juptyer notebook


Variables (「變」數)

> 替不同的物件命名 (name or label an object)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
在一個程式裡,時常會存在多個相同類別的物件;如:他開Toyota, 你開Benz 等。


如何命名一個物件?

利用 assignment (賦值) operator: =
>

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
不是指數學的"等於" 呦!

  • 而是將物件 指定 給一個變數
    • 此時,變數就是該物件的一個 reference (參考):透過變數來存取該物件
    • 如:
      image

Referenes 與 Variables 的關係

image


為什麼叫「變」數?

  • 不同執行時間,同一個變數可以用於參考不同的物件;如:

    image

  • 亦可用於改變物件的狀態 (mutate)

    image

    • 新增 '4' 到 a 指向的 list

Assignment (賦值) 是如何發生的呢?

  • Python 會先求出 RHS 的值,再將該值「賦」
    image

    • LHS: Left-hand side
      RHS: Right-hand side

  • 一般來說,RHS 會是一個比文字更複雜的表示式;如:
    image


變數命名的方式

  • case sensitive: 相同英文字母但大小寫不同,被視為"不同"的變數
    • 必須 遵從特定的規格:否則編譯器會回報錯誤
    • 應該 遵從特定的慣例:編譯器不一定會回報錯誤,主要用意是讓不同的人都能理解相同的程式

"必須" 遵從特定的規格

image


"應該" 遵從特定的慣例

image


  • PEP 8 Style Guide: https://www.python.org/dev/peps/pep-0008/
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    為什麼是 8?
    PEPPython Enhancement Proposal 的縮寫;PEP 有多個主題,而主題是用後面的數字來索引。

  • 慣例是拿來打破的,只要有好的原因且ㄧ致

Coding

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Let's move to the 04 - Variables juptyer notebook


Arithmetic Operators


Terminology (術語)


  • 用於運算子上的數個值稱為 operands: 運算
    依據運算元的數目,運算子可再分為:
    • unary operator: 一元運算子; 'u' 意指 "單一",指只有一個運算元的運算子,如代表負數的「-」號。
    • binary operator: 二元運算子
    • ternary operator: 三元運算子

Arithmetic Operators

image


Operand Types

  • 運算子的類型決定運算子的運算內容
    image

The Power Operator

某基數的操作,如:

  • 基數為整數搭配正或負的冪次
    image


Python 如何實作算數運算子?

  • Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    數字實際上就是物件
    • 物件就會有 狀態功能,其中一個常用的功能就是 __add__ 函式
  • 如:
    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

任何一個類型都可以實作自己的 add 函式

  • 當我們使用 "+" 運算元時,Python 會呼叫該類型的 __add__ 函式,如:
    image

Coding

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Let's move to the 05 - Arithmetic Operator juptyer notebook

Select a repo