Just Make It
      • 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
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners 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
    • 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 Help
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
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners 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
    # 變數概念 ###### tags: `Python` ## 型別 > 1. `Python` 中,變數分3種 `Type 型別` ,分別是 `int 整數`、`str 字串`、`float 浮點數`。 > 2. `Python` 是一種 `弱型別語言` ,它會自行偵測 `變數` 型態,但也可以手動調整,甚至可以將 `變數` 從 `int` 變成`str`。 > 3. `str 字串`內的單一文字稱為`字元` ### integer 整數 ```python= print(10) ``` - 可以試著丟進去 `Python 編譯器` 裡試看看 ![截圖 2024-03-08 下午1.56.03](https://hackmd.io/_uploads/S11KdQ_pT.png) ### string 字串 ```python= print('Hello World !') ``` - 可以試著丟進去 `Python 編譯器` 裡試看看 ![截圖 2024-03-08 下午2.39.01](https://hackmd.io/_uploads/r1b5z4daa.png) > print('你要輸入的內容') > ![截圖 2024-03-08 下午2.55.27](https://hackmd.io/_uploads/rynv8EO6a.png) ### float 浮點數(小數) ```python= print(3.14159) ``` - 可以試著丟進去 `Python 編譯器` 裡試看看 ![截圖 2024-03-08 下午3.04.05](https://hackmd.io/_uploads/rJM_uVO6a.png) ### `type() 函數` > 用於協助判斷括號內的型別,如以下: ``` python= print (type (10)) print (type ('Hello World !')) print (type (3.14159)) # 輸出結果: # int # str # float ``` ## 變數 > 可視為變數為一種`暗號` -- -- > 而`暗號`所對應的東西需要被定義,此暗號才會成立。(本質上就是數學運算中的未知數,只是可以在程式運行中隨意更改其值) ```python= x = 10 # x = 暗號名字 # 10 = 暗號對應(也代表此暗號之後只能代表整數 ``` > 備註(一):之後也可用 `指令` 更改暗號 `型別` > 備註(二):準確說法,在此程式碼中,`變數x` `被賦值為` `10` ### 試試這段程式碼 ``` python= x = 10 print (type (x)) # 輸出結果: # <class 'int'> ``` > 備註:在 `Shell` 中,不使用 `print()` 也可輸出 `型別` ### 變數的型別 > 變數也有三種型別,分別是 `int 整數` 、`str 字串` 、`float 浮點數`;前面示範的正是 `int 整數`,接下來介紹`str 字串` 和 `float 浮點數`。 ```python= x = 'Hello World !' y = 3.14159 print (type (x)) print (type (y)) # 輸出結果: # <class 'str'> # <class 'float'> # 可以試試 z = '3.14159' 再去窺探他的型別,就會了解為什麼需要分類 ``` ## 四則運算 ### int 整數 ```python= x = 10 y = 2 print (x + y) #加法 print (x - y) #減法 print (x * y) #乘法 print (x // y) #除法(求商) print (x % y) #除法(求餘) print (x / y) #除法(除盡)(輸出為浮點數) print (x * 2 + y * 2) #遵從先乘除後加減 # 輸出結果: # 12 # 8 # 20 # 5 # 0 # 5.0 # 24 ``` ### str 字串 ```python= x = '123' y = '456' print (x + y) print (x * 2) print (x * 2 + y * 2) # 輸出結果: # 123456 # 123123 # 123123456456 ``` - 需注意 `str 變數` 只能做疊加使用 ### float 浮點數 ```python= x = 3.14159 y = 6.02214 print (x + y) #加法 print (x - y) #減法 print (x * y) #乘法 print (x // y) #除法(求商) print (x % y) #除法(求餘) print (x / y) #除法(除盡)(輸出為浮點數) print (x * 2 + y * 2) #遵從先乘除後加減 print (type(0.5 * 2)) #原則上浮點數乘以整數仍是浮點數(就算為整數樣貌) # 輸出結果: # 9.163730000000001 # -2.8805500000000004 # 18.9190948026 # 0.0 # 3.14159 # 0.5216733586399519 # 18.327460000000002 # <class 'float'> ``` - 備註: 數字具有`.0`皆為浮點數 ## 注意事項 1. - 在 `Python` 中,加減乘除跟一般的時候一樣;但須注意`程式語言`的規則中,每次更改 `變數` 內的 `物品` 時,它都需要重新確認裡面的物品,也就是每次都要重新 `賦值`,以下範例: ```python= x = 10 print (x) # 輸出結果: # 10 x = x + 10 print (x) # 輸出結果: # 20 ``` - 在第二行中,第一個 `x` 是要重新定義的`變數`,第二個則是重新定義前的`變數`;也就是在第二行實際上是`x = [10] + 10`。 > 備註: > - 在 `Python` 中, ` x += 10 ` 、 ` x -= 10 ` 效果等同於 ` x = x + 10 ` 、 ` x = x - 10 ` , 但在不同語言不一定可行( Python , C , C++ , Swift , Java , JavaScript 都行 ) 2. - 變數的命名和數學計算中的 `未知數` 一樣,可以隨意命名(但不能和程式內建的 `函數` 同名就是了),但大致上還是希望其具有含義,使其可以在具有多行數的程式中更好被判讀;而常用的是 `小駝峰命名` ,以小寫的單字為開頭,遇到下個單字時,開頭改為大寫(例如:firstName)。 3. - 變數的活用才是程式的精髓,比如以下 `int 整數型別` 的範例程式碼,只需將 `x = 10` , `y = 2` 更改,就可以進行不同組數字間的運算,而不用逐行修改程式碼。 ```python= print (10 + 2) #加法 print (10 - 2) #減法 print (10 * 2) #乘法 print (10 // 2) #除法(求商) print (10 % 2) #除法(求餘) print (10 / 2) #除法(除盡)(輸出為浮點數) print (10 * 2 + 2 * 2) #遵從先乘除後加減 ``` - 這段程式碼當然輸出結果與 `int 整數型別` 一模一樣,但我需要將(10 , 2)改成(60 , 15)時,就要費一段功夫,而在 `int 整數型別` 中只需要更改 `x` , `y` 的賦值,如以下: ```python= x = 60 y = 15 print (x + y) #加法 print (x - y) #減法 print (x * y) #乘法 print (x // y) #除法(求商) print (x % y) #除法(求餘) print (x / y) #除法(除盡)(輸出為浮點數) print (x * 2 + y * 2) #遵從先乘除後加減 ```

    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