Kyle Hsieh (謝阿Sa)
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 大綱 - 基礎 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

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully