Peter Yang
    • 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
    • Make a copy
    • 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 Make a copy 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
    ###### tags: `python` <style> blue:color='blue'; </style> # Python 基本語法 最近修改日期20210108 #### 變數: 語法:變數名稱=變數值 Python變數無須宣告及指定資料型態 多個變數有相同值 a=b=c=20 同一列指定鍋的變數值 age,name = 10,"張三" 則 age=18 ,name= "張三" del 變數名稱 #刪除變數節省記憶體 變數名稱命名規則 *.第一個字母需大小寫英文字母、_、中文。(數字開頭不行) *.變數只能由大小寫英文字母、_、中文組成。(特殊符號不可以,中間也不可空白) *.英文大小寫視為不同變數。(區分大小寫) *.不能與保留字相同。 保留字有 ||||||| |-|-|-|-|-|-| |acod|and|array|asin|assert|atan| |break|class|close|continue|cos|Data| |def|del|e|elif|else|except| |exec|exp|fabs|float|finally|floor| |for|from|global|if|import|in| |input|int|is|lambda|log|log10| |not|open|or|pass|pi|print| |raise|range|return|sin|sqrt|tan| |try|type|while|write|zeros|| \# 為註解符號 \\ 為程式碼過長換行接續符號 Python 數值資料型態主要有整數、浮點數。 布林資料型態 只有True 與 False 字串可用成對的" 或是 ' 包起來。 若要表示換行或是Tab可用跳脫字元\ |跳脫字元|意義|跳脫字元|意義| |-|-|-|-| |\\'|顯示單引號'|\\"|顯示雙引號"| |\\\ |顯示反斜線|\\n|換行| |\r|游標移到列首|\t|Tab鍵| |\v|垂直定位|\a|響鈴| |\b|後退鍵|\f|換頁| |\x|以16進位表示字元|\o|以八進位表示字元| ## print語法: 語法: print(項目1 [,項目2,...,sep=分隔字元,end=結束字元] ) 範例: ```python= print(10,"項目",50) # 輸出 10 項目 50 print(10,"項目",50,sep="&") # 輸出 10&項目&50 下次列印於下一列 print(10,"項目",sep="&",end="") # 輸出 10&項目&50 下次列印於同一列(註:end沒指定,預設為\n) ``` ### 整理後的格式化輸出 ```python= # 範例_字串格式化 name='Peter Yang' errno=50159747054 # 舊式 print('舊式') print('你好! %s'% name) # %x 16進位表示法 print('嗨! %s, 這個錯誤代碼是%x'%(name,errno)) print('嗨! %(name)s, 這個錯誤代碼是%(errno)x'%{'name':name,'errno':errno,}) print('嗨! %(name)s, 這個錯誤代碼是%(errno)x'%{'errno':errno,'name':name,}) # 新式 print('新式') print('Hello,{}'.format(name)) print('嗨! {name}, 這個錯誤代碼是{errno:x}'.format(name=name,errno=errno)) # 進階(格式字串字面值:Formatted String Literal Py3.6+ UP) print('進階') print(f'Hello,{name}!') birthYear=25 print(f'Hello,{name},出生年{birthYear}轉為西元為{birthYear+1911},16進制為{(birthYear+1911):x}') # 可以進行運算 (可以用x 或是 #x) # 樣板字串方法(Template String) from string import Template t= Template('Hello,$name!') t.substitute(name=name) print(t.substitute(name=name)) t1="Hello! $name 有一個錯誤發生 $errno" t2=Template(t1) print(t2.substitute(name=name,errno=hex(errno))) # 樣板無法此用格式指定子,要自行用hex轉換。 ``` ### 格式化輸出(一) 語法: print(項目 %(參數)) #%s 表示字串,%d表示整數,%f表示浮點數 可以用%5d 固定列印5位數。數字小於5位,左方填空白,大於5位,全部顯示。 %5s 同上 %8.2f固定列印8的字元(含小數點),小數點固定兩位。整數若小於5位,左方填空白,整數大於5位,則整數部分全部顯示;若小數小於2位,右方填0,小數大於2位,原則四捨五入(會依據第一位小數奇偶而變化) n=1222.3350 print("%8.2f" % n) # 1222.34 n=1222.2350 print("%8.2f" % n) % 1222.33 範例: name="張三" age = 58 print("%s的年紀為%d歲" % (name,age)) ### 格式化輸出(二) 語法: print(format(item,format-specifier)) item 為數字或是文字,format-specifier:格式指定器。 範例: ```python= name="張三" age = 58 print(format(name,'8s'),format(age,'3d')) # 張三   58 ``` 格式指定器: |指定器|說明| |-|-| |d|整數| |f|浮點數| |s|字串| |e|科學記號| |x|16進位表示| |o|8進位表示| |b|2進位表示| |%|百分比表示| |<|靠左對齊| |>|靠右對齊| 指定器前方可以加入數字,表示欄位寬度 範例: ```python= print(format(100,'8d')) # 100 由第8格往左排(預設靠右) print(format(100,'<8d'),end='|') #100 |靠左對齊空8格 print(format(100,'8x')) print(format(100,'8o')) print(format(100,'8b')) # 64 # 144 # 1100100 print(format(0.2389,'6.2%')) #23.89% 留6個位置,%小數留2位。 print(format(123.456,'8.2f')) # 123.46 留8個位置,小數點2位。 ``` print(字串.format(參數列)) ```python= name="張三" age = 58 print("{}的年紀為{}歲".format(name,age)) #張三的年紀為58歲 print(format(name,'8s'),format(age,'3d')) # 張三   58 ``` 另外一種寫法 ```python= name='Mike' func_id='func_A01' print(f'{name} use the {func_id}') #Mike use the func_A01 ``` 使用type取地目前資料型態 ```python= print(type(56)) # <class 'int'> print(type("張三")) # <class 'str'> print(type(True)) # <class 'bool'> ``` S20200721 By YTC M20200908 補充與調整, 20201102 M20210108 補充整理後的資料

    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