Oscar Lin
    • 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
    • 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 Versions and GitHub Sync Note Insights 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
    1
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- title: Python 新手導覽( 基本功能篇 ) tags: Python, note, guiding --- # Python 基本功能 [TOC] ## 輸入輸出 > 對於電腦來說 輸出輸入可以透過很多不同的裝置 > 比如 透過鍵盤/ 滑鼠 輸入 > 透過螢幕/ 喇叭輸出 等 > 而現在的輸入 指的就是使用者透過鍵盤(STDIN) 輸入 > 輸出指的就是 螢幕上顯示出的文字(STDOUT) > (括弧內不用理他 有興趣再去看就好) > [color=blue] ### input() > `a = input()` or `a = input('type something')` > a: str > [color=blue] > 透過此函式(數),就能接收使用者輸入的文字(以 enter 結尾) > 所以當程式執行到 `a = input()`,就會停下來等待鍵盤的輸入資料,直到出現 enter 鍵 > 之後把這一串資料,做為==字串==存到前面的變數中 a > e.g. 輸入 123 456 之後,變數 `a` 就會變成字串 `'123 456'` ### print() > `print(content1, c2, c3, sep=' ', end='\n')` > 最後的結果會像這樣: content1 + sep + c2 + sep + c3 + end > [color=blue] > 透過此函式(數),能將資料輸出到螢幕上 > 可以輸出各種形態的資料,輸出兩種資料以上時 請用逗號分隔`print(1, '2')` > 輸出結果會在資料間加上空白鍵`1 2` > 若想更改預設的間隔及結尾符號在最後輸入 sep/end 的數值即可 > `print('hello', 'world', sep=' ~ ', end='!\n') # hello ~ world!` > 補充: `'\n'` 在大部分程式中都是換行字元的意思,所以若要換兩行就是輸出 `'\n\n'` ## 程式架構 > 循序/ 選擇/ 重複 結構是程式最重要的 ### if ```python= if 條件成立: code... elif 上面的條件失敗 不過這個條件成功: code... else: # 上面都失敗了 就執行他 code # 離開 if 區塊 ``` #### if 這麼多種 怎麼組合才是對的 > ==if + elif(*0~n) + else(*0~1)== > if > if + elif > if 一定要有 放在最前面 | 後面可以加 0~n 個 elif | 最後可以加 else > \# 可以的意思就是 可加可不加 可不可(X ### for ```python= for e in range(10): code... # 離開 for 區塊 ``` ### while ```python= while 條件成立: code... # 離開 while 區塊 ``` ## 型態 ### 整數 (int) `a = 1` ### 浮點數 (小數 float) `a = 3.23` 可以*特別留意一個問題,程式中的小數並不是完全"精準"的, 有興趣的話可以去搜尋看看 `0.1 + 0.2` https://0.30000000000000004.com/* ### 字串 (string) `a = "hello"` `a = 'hello'` ### 布林 (bool) `a = True` `a = False` ## 程式運算 ### (數值, 數值) -> 數值 四則運算 + 與除法相關的 `%` 跟 `//` + 次方 `**` - `+` : 加 - `-` : 減 - `*` : 乘 - `/` : 除 - `%` : 取餘數 - `//` : 整數除法 ``` 9 除以 4 等於 2 餘 1 等於 2.25 print(9 / 4) -> 2.25 print(9 % 4) -> 2 print(9 //4) -> 1 ``` - `**` : 次方 `print(3 ** 2) -> 9` ### (數值, 數值) -> 布林 - `==` : 等於 - `!=` : 不等於 - `<` : 小於 - `<=` : 小於等於 / 不大於 - `>` : 大於 - `>=` : 大於等於 / 不小於 ### (布林, 布林) -> 布林 - `and`, `&` -> `if(A and B)`, `if(A & B)` - `or`, `|` -> `if(A or B)`, `if(A | B)` - `not`, `!` -> `if(not A)`, `if(!A)` - `^` : Xor(exclusive or) -> `if(A ^ B)` #### 真值表補充 *用來表示 `True`, `False` 在經過運算過後,會變為什麼值的表格* ::: spoiler 展開 *以 O 為 `True`,以 X 為 `False`* | A | B | A and B | | --- | --- | ------- | | O | O | ==O== | | O | X | X | | X | O | X | | X | X | X | | A | B | A or B | | --- | --- | ------ | | O | O | O | | O | X | O | | X | O | O | | X | X | ==X== | | A | not A | | --- | ----- | | O | X | | X | O | | A | B | A Xor B | | --- | --- | ------- | | O | O | O | | O | X | X | | X | O | X | | X | X | O | ::: ## 函式 - 不含輸入、回傳 ```python= def func(): print("Hello, world!") ``` - 輸入 ```python= def func(var): print('input a', var) ``` - 輸出 ```python= def func1(): return 'the function output' def func2(): return 123 ```

    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