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 New
    • 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 Note Insights Versions and GitHub Sync 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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 輸出結構 ###### tags: `Python` ## `print ( ) 函數` > `函數` 可以視為程式的快捷鍵;以 `print( )` 函數為例,只要輸入 `print(想輸出的東西)` ,它就可以幫你 `print` 出來,而程式語言中存在這種各式各樣,各種功能的函數,在未來就會碰到了 ## 普通輸出 > 在Python中輸出變數時,可以指定一些格式,例如... ### int 整數 ``` python= n = 10 print('我有%d個喜歡的遊戲' % n) # 輸出結果: # 我有10個喜歡的遊戲 ``` #### 或是... ``` python= n = 10 m = 3 print('我有%3d個朋友,我們每週會一起玩%2d次Apex' % (n,m)) # 輸出結果: # 我有 10個朋友,我們每週會一起玩 3次Apex # %3d表示指定輸出3位數字(未滿則以空格填補 ``` ### str 字串 ``` python= n = 'Mr.' print('我的綽號是%s' % n) # 輸出結果: # 我的綽號是Mr. ``` ### float 浮點數 ``` python= n = 3.14159 print('圓周率到小數點後四位是%.4f' % n) #小數點前位數也可指定 例:%2.4f # 輸出結果: # 圓周率小數點前四位是3.1416 ``` ### 外加... #### print 換行 > 加入`\n`會判別為需要換行 ``` python= print('我有10個喜歡的遊戲\n我有10個朋友,我們每週一起玩3次Apex') # 輸出結果: # 我有10個喜歡的遊戲 # 我有10個朋友,我們每週一起玩3次Apex ``` #### print 不換行 > 加入`,end=''`會判別為輸出結尾應為`''` ``` python= print('我有10個喜歡的遊戲', end='') print('我有10個朋友,我們每週一起玩3次Apex') # 輸出結果: # 我有10個喜歡的遊戲我有10個朋友,我們每週一起玩3次Apex # 預設為: # print('我有%d個喜歡的遊戲' % n , end='\n') ``` 或是 > 將輸出內容進行拆分 ``` python= n = 10 m = 12 print ('我有%d個喜歡的遊戲,' % n, '同時,', '我有%d個朋友會跟我一起玩' % m) # 輸出結果: # 我有10個喜歡的遊戲, 同時, 我有12個朋友會跟我一起玩 ``` #### print 空格 > 加入`\t`會判別為需要輸出同`tab鍵`的空格 ``` python= print('我有10個喜歡的遊戲\t我有10個朋友,我們每週一起玩3次Apex') # 輸出結果: # 我有10個喜歡的遊戲 我有10個朋友,我們每週一起玩3次Apex ``` #### print 特殊符號 ```python= print('\\') print('\'') print("\"") # 輸出結果: # \ # ' #" ``` #### print 對齊(靠右) ```python= n = 'name=' print('[%10s]' % n) # 輸出結果: # [ name=] ``` #### print 對齊(靠左) ```python= n = 'name=' print('[%-10s]' % n) # 輸出結果: # [name= ] ``` ### print 多行 ```python= print('''Hi 我是 Harry! ''') # 輸出結果: # Hi # 我是 Harry! ``` ## format 格式化輸出 ### int 整數 ``` python= n = 123 m = 456 print('{} {}'.format(n, m)) # 不數字編號 print('{0} {1}'.format(n, m)) # 帶數字編號(程式語言順序由0開始) print('{1} {0}'.format(n, m)) # 自訂順序 print('{0} {1} {0}'.format(n, m)) # 重複多次輸出 print(f'{n} {m}') # 直接指定 # 輸出結果: # 123 456 # 123 456 # 456 123 # 123 456 123 # 123 456 ``` ### str 字串 ``` python= n = Hello m = World print('{} {}'.format(n, m)) # 不數字編號 print('{0} {1}'.format(n, m)) # 帶數字編號(程式語言順序由0開始) print('{1} {0}'.format(n, m)) # 自訂順序 print('{0} {1} {0}'.format(n, m)) # 重複多次輸出 print(f'{n} {m}') # 直接指定 # 輸出結果: # Hello World # Hello World # World Hello # Hello World Hello # Hello World ``` ### float 浮點數 ``` python= n = 3.14159 m = 6.02214 print('{} {}'.format(n, m)) # 不數字編號 print('{0} {1}'.format(n, m)) # 帶數字編號(程式語言順序由0開始) print('{1} {0}'.format(n, m)) # 自訂順序 print('{0} {1} {0}'.format(n, m)) # 重複多次輸出 print(f'{n} {m}') # 直接指定 # 輸出結果: # 3.14159 6.02214 # 3.14159 6.02214 # 6.02214 3.14159 # 3.14159 6.02214 3.14159 # 3.14159 6.02214 ``` ### 外加... #### print 對齊(靠右) ```python= n = 'name=' print('[{:10}]'.format(n)) # 輸出結果: # [ name=] ``` #### print 對齊(靠左) ```python= n = 'name=' print('[{:>10}]'.format(n)) # 輸出結果: # [name= ] ``` #### print 對齊(置中) ```python= n = 'name=' print('[{:^10}]'.format(n)) # 輸出結果: # [ name= ] ``` # 輸入結構 ## input()函數 ### int 整數 ```python= n = input('請輸入你的年齡:') print(n) # 輸出結果: # 你的年齡 ``` ### str 字串 ```python= n = input('請輸入你的名字:') print(n) # 輸出結果: # 你的名字 ``` ### float 浮點數 ```python= n = input('請輸入圓周率到小數點後2位:') print(n) # 輸出結果: # 3.14 ``` ### 請試試輸出三段程式的n的型別... ```python= n = input('請輸入你的年齡:') print(n) print(type(n)) # 輸出結果: # 你的年齡 # <class 'str'> ``` ```python= n = input('請輸入你的名字:') print(n) print(type(n)) # 輸出結果: # 你的名字 # <class 'str'> ``` ```python= n = input('請輸入圓周率到小數點後2位:') print(n) print(type(n)) # 輸出結果: # 3.14 # <class 'str'> ``` #### 沒錯,皆是`str 字串` > 這是Python的設定,但也可以將`str 字串`調整成正確的型別 ## `int() 函數` ```python= n = input('請輸入你的年齡:') n = int(n) print(n) print(type(n)) # 輸出結果: # 你的年齡 # <class 'int'> ``` ## `str() 函數` ```python= n = 3.14159 n = str(n) print(n) print(type(n)) # 輸出結果: # 3.14159 # <class 'str'> ``` ## `float() 函數` ```python= n = input('請輸入圓周率到小數點後2位:') n = float(n) print(n) print(type(n)) # 輸出結果: # 3.14 # <class 'float'> ``` ## `eval() 函數` > 上述三個函數是強制將變數轉換成指定的型別;但如果不確定變數是`int 整數`還是`float 浮點數`時,直接用`int() 函數`會把`float 浮點數`的`小數`去掉,強制轉換成`int 整數` > >而`eval() 函數`則是會自動判斷需要轉換成`int() 函數`還是`float 浮點數` ```python= n = input('請輸入你的年齡:') n = eval(n) print(n) print(type(n)) # 輸出結果: # 你的年齡 # <class 'int'> ``` ```python= n = input('請輸入圓周率到小數點後2位:') n = eval(n) print(n) print(type(n)) # 輸出結果: # 3.14 # <class 'float'> ``` ## `split() 函數` > 在需要單行輸入多個變數的值時,可以使用`.split() 函數`,它會根據`()`內要求分割,並依順序賦值於變數 ``` python= n,m = input('輸入生日的月份及日期,範例5/13:').split('/') print(n) print(m) # 輸出結果: # 月份 # 日期 # 預設為: # n,m = input('輸入生日的月份及日期,範例5/13:'.split('') ```

    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