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

      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 --- # DAY 04 串列的操作與運用 昨天我們討論到如何一一存取串列中的元素,今天我們要來學的是如何用迴圈印出串列中的所有元素,一開始我們會先學到**for迴圈**,那麼甚麼是for迴圈呢 ? for迴圈在電腦科學是一種程式語言的疊代陳述,能夠讓程式碼反覆的執行,它跟其他的迴圈,如while迴圈,最大的不同,是它擁有一個迴圈計數器,或是迴圈變數。這使得for迴圈能夠知道在疊代過程中的執行順序 範例如下 : ```python= students=["bonny","jack","rose"] # 建立一個students串列 for student in students : # 告知python從students串列中取出名字,並將取出的名字存到student變數內 print(student) # 印出student變數取到的名字 print("hello !") # 跳出迴圈並印出hello ``` 輸出結果 : ```python bonny jack rose hello ! ``` :bulb:所以說for迴圈到底是怎麼運作的 ?為甚麼可以一一擷取串列中的元素然後印出 ? 其實迴圈它是這樣跑的,在程式執行的一開始先讀到 ```python for student in students : ``` 這行程式會讓python去students串列中擷取第一個值,也就是"bonny"這個值,將它存入student變數內後,python會跑到下一行將student變數存到的值印出 ```python print(student) # 印出bonny ``` 因為students串列中還有其他的元素,所以在印出student變數的值過後,python會再跳回for迴圈去印其他的值 ```python for student in students : ``` 在跳回for迴圈的後,python會讓for迴圈去擷取第二個值,也就是"jack",擷取到一個值將它存到student變數中過後,就跳到迴圈下一行將其印出 ```python print(student) #印出jack ``` 當for迴圈擷取完串列裡所有的元素後,python就會跳出迴圈,然後再印出迴圈外的hello字串 :warning: 迴圈內執行的東西要記得縮排,若不是迴圈內要執行的東西則不用縮排 ! --- ## 使用range()函式建立數字串列 range()函式能輕鬆的產生一系列的數字 範例如下 : ```python= for number in range(1,7): print(number) ``` 上面這段程式看起來是要印1到7,但實際上它是印1到6,7是不會被印出的,因為**range()函式會讓python從指定的第一個值開始算起,到指定的第二個值就停止,因此輸出並不含第二個值** 輸出結果: ```python 1 2 3 4 5 6 ``` 若是要讓數字存到串列中,可以使用**list()函式**把range()函式的結果直接轉成串列,用法是把range()函式當成參數包在list()函式裡 範例如下 : ```python= odd_numbers=list(range(1,10,2)) print(odd_numbers) ``` 上面這段程式碼是要**印出1~10之間的奇數**,第一行程式碼的range(1,10,2)是在說建立一個從**1**開始間隔是**2**到**10**就停止的數列,然後再用list()函式包住range()函式,使range()函式建立出來的數列變成數字串列,第二行就是把值印出 輸出結果 : ```python [1,3,5,7,9] ``` --- ## 擷取串列中的某部分 擷取串列中某部分的內容我們在python中稱之為**切片**,切片的用法是**需要指定使用的第一個元素和最後一個元素的索引足標**,和range()函式用法一樣,切片的處理範圍不含第二個索引足標的元素 **但是有兩點要注意 : 1.沒有指定第一個元素的索引足標則從0開始計算 2.沒有指定最後一個元素的索引足標則視為切到最後一位** 範例如下 : ```python= colors=["red","orange","yellow","green","blue"] print(colors[0:3]) # 印出索引足標0,1,2對應的元素 print(colors[1:4]) # 印出索引足標1,2,3對應的元素 print(colors[:2]) # 印出從索引足標0到1的元素 print(colors[2:]) # 印出從索引足標2到最後的元素 print(colors[-2:]) # 印出從索引足標-2(也就是倒數第二個)到最後的元素 ``` 輸出結果 : ```python ["red","orange","yellow"] ["orange","yellow","green"] ["red","orange"] ["yellow","green","blue"] ["green","blue"] ``` --- ## 切片的迴圈應用 * for迴圈印出切片的串列 範例如下 : ```python= colors=["red","orange","yellow","green","blue"] for color in colors[1:3] : # 印出colors串列中索引足標1和2的元素 print(color) ``` 上面程式碼用法和一開始說的用for迴圈印串列大致上是一樣的,不一樣的地方只有它會告訴python是在**切片串列中取出元素**,然後再存到變數中,所以輸出結果如下 : ```python orange yellow ``` * 複製串列 有些時候我們會以一個串列為基底,然後去建立一個新的串列,複製串列在python中的做法就是不放起始和結尾的索引足標,只留一個冒號在括號中,這樣就可以複製整個串列了 範例如下 : ```python= my_pets=["dog","cat","bird"] # 建立一個my_pets串列 friend_pets=my_pets[:] # 將my_pets串列複製到friend_pets串列 print("My pets :") print(my_pets) # 印出my_pets串列 print("Friend's pets :") print(friend_pets) # 印出friend_pets串列 ``` 輸出結果 : ```python My pets : ["dog","cat","bird"] Friend's pets : ["dog","cat","bird"] ``` 為了證明這分別是兩個串列,我們也可以個別新增元素到my_pets跟friend_pets裡 範例如下 : ```python= my_pets=["dog","cat","bird"] # 建立一個my_pets串列 friend_pets=my_pets[:] # 將my_pets串列複製到friend_pets串列 my_pets.append("rabbit") # 新增一個元素到my_pets串列 friend_pets.append("turtle") # 新增一個元素到friend_pets串列 print("My pets :") print(my_pets) # 印出my_pets串列 print("Friend's pets :") print(friend_pets) # 印出friend_pets串列 ``` 輸出結果 : ```python My pets : ["dog","cat","bird","rabbit"] Friend's pets : ["dog","cat","bird","turtle"] ``` --- ## 甚麼是多元組 ? 有時候我們會建立一個**不能修改**的資料集合,而這個**不可變的串列**我們就叫它**多元組**,多元組跟串列很像,唯一不同的是串列是用中括號[ ],**多元組是用小括號( )**,定義完多元組後我們可以用索引足標來取得元素,做法和串列相同 * 定義多元組 我們可以舉個生日的例子來建立一個多元組,因為生日是不會被改變的 ! 範例如下 : ```python= birthday=("1224","0801","0617") # 定義一個多元組 print(birthday[0]) # 印出多元組第一個元素 ``` 輸出結果 : ```python 1224 ``` * 用迴圈拜訪多元組 處理方式和串列一樣,我們使用for迴圈來拜訪多元組 ```python= birthday=("1224","0801","0617") # 定義一個多元組 for day in birthday : # 在birthday多元組中取得元素再存到day變數裡 print(day) # 印出變數存到的值 ``` 輸出結果 : ```python 1224 0801 0617 ``` * 如何更改多元組的值 多元組是無法直接被更改的,如果想要改變原本多元組的值,我們可以重寫它,就是可以把新的值指定給存放多元組的變數內 範例如下 : ```python= numbers=(1,2,3) # 定義一個多元組 print("Original :") for number in numbers : # 在numbers多元組中取得元素再存到number變數中 print(number) # 印出變數存到的值 numbers=(4,5,6) # 讓原本多元組(1,2,3)的值改為(4,5,6) print("Modified :") for number in numbers : print(number) ``` 輸出結果 : ```python Original : 1 2 3 Modified : 4 5 6 ``` #### 今天結束,各位明天見 :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