Wei-Ting
    • 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
    • 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
    • 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 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
  • 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- tags : python --- # DAY 14 函式應用( 2 ) ## 傳入任意數量的引數到函式 有時候我們不知道函式預先需要接收多少個引數,這時候我可以使用**一個星號**來建立空多元組,或**兩個星號**建立空字典,我們就先直接來看範例吧 ! * 一個星號(建立空多元組) 範例如下 : ```python= def get_name(*names): # *names參數中的星號會讓python建立一個名字為names的空多元組 for name in names: print("hello,"+name) get_name("bonny","steven") get_name("jack") get_name("rose","john","jane") ``` 輸出結果 : ```python hello,bonny hello,steven hello,jack hello,rose hello,john hello,jane ``` :bulb: 如果上述的程式碼沒有for迴圈那行,那麼程式碼就會出錯,因為for迴圈的工作是把多元組裡的值一一輸出出來,所以如果沒有for迴圈那行而直接印出的話,是印出多元組而不是印出接收到的字串值,多元組是不能跟字串相加的,所以它會跑出下列這種錯誤 >TypeError: can only concatenate str (not "tuple") to str * 兩個星號(建立空字典) 範例如下 : ```python= def users(first_name,last_name,**user_info): # **user_info參數中的星號會讓python建立一個名字為user_info的空字典 user={} user["first"]=first_name # 將first_name新增至user字典 user["last"]=last_name # 將last_name新增至user字典 for key,value in user_info.items(): # 用迴圈遍訪user_info裡的鍵值對並將其新增至user字典 user[key]=value return user user1=users("bonny","chang",city="taipei") print(user1) user2=users("steven","chang",city="taoyuan") print(user2) ``` 輸出結果 : ```python {'first': 'bonny', 'last': 'chang', 'city': 'taipei'} {'first': 'steven', 'last': 'chang', 'city': 'taoyuan'} ``` ## 將函式存到模組中 函式的優點是他們可以跟主程式分開放置,所以將函式儲存在一個獨立的檔案中,這個檔案就稱為**模組** * ==匯入整個模組== import name會讓python在執行這行指令時開啟name.py檔,並將其都複製到get_name.py檔中,這樣get_name.py檔才可以用get_name函式,呼叫函式的用法是給定模組名稱和函式名稱,中間以句號(.)分隔連接就可以了! ↓ name.py檔 ```python= def get_name(*names): for name in names: print("hello,"+name) ``` ↓ get_name.py檔 ```python= import name name.get_name("bonny") name.get_name("jack","rose") ``` 輸出結果 : ```python hello,bonny hello,jack hello,rose ``` * ==使用as為模組指定別名== 使用as其實只是為模組取一個叫短的別名,方便我們使用 ↓ name.py檔 ```python= def get_name(*names): for name in names: print("hello,"+name) ``` ↓ get_name.py檔 ```python= import name as n n.get_name("bonny") n.get_name("jack","rose") ``` 輸出結果 : ```python hello,bonny hello,jack hello,rose ``` * ==匯入特定函式== 假如name.py檔裡有兩個函式,但我們只需要get_name函式時,我們就要匯入特定函式,如果想要匯入多個函式,可以用逗號分隔,呼叫的方式不用再寫出模組名稱,只需要給定函式名稱就好 ↓ name.py檔 ```python= def get_name(*names): for name in names: print("hello,"+name) def get_city(*cities): for city in cities: print(city) ``` ↓ get_name.py檔 ```python= from name import get_name get_name("bonny") get_name("jack","rose") ``` 輸出結果 : ```python hello,bonny hello,jack hello,rose ``` * ==使用as為函式指定別名== ↓ name.py檔 ```python= def get_name(*names): for name in names: print("hello,"+name) def get_city(*cities): for city in cities: print(city) ``` ↓ get_name.py檔 ```python= from name import get_name as gn gn("bonny") gn("jack","rose") ``` 輸出結果 : ```python hello,bonny hello,jack hello,rose ``` * ==匯入模組中所有函式== 用星號( * )運算子可以讓python匯入模組中所有的函式 ↓ name.py檔 ```python= def get_name(*names): for name in names: print("hello,"+name) def get_city(*cities): for city in cities: print(city) ``` ↓ get_name.py檔 ```python= from name import * get_name("bonny") get_city("taipei") ``` 輸出結果 : ```python hello,bonny taipei ``` #### 今天結束,各位明天見 :hand: *** 資料來源:<<python程式設計的樂趣>>-Eric Matthes著/H&C譯

    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