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
      • 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 Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync 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
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 03 認識串列 ## 串列的使用 「串列」是個依照特定順序排放的項目集合所組成,在python中串列是用中括號[ ]來表示,而串列中的個別元素是以逗號來分隔 以下為一個簡單的範例 : ```python= fruits=["apple","banana","grape","pineapple"] print(fruits) ``` 上面程式碼的第一行是先建立一個裡面有四個元素的fruits串列,第二行是把整個串列印出來,印出結果如下 : ```python ["apple","banana","grape","pineapple"] ``` 如果說只想要存取一個元素來輸出的話,就得寫出==串列的名稱並標記它的索引值== :warning: ==索引值是從0開始,不是1==,索引值0的意思就是第一個元素,索引值1的意思就是第二個元素,索引值2的意思就是第三個元素...以此類推 範例如下 : ```python= fruits=["apple","banana","grape","pineapple"] print(fruits[0]) # 這行的意思是印出fruits串列中索引值0的位置,也就是第一個元素 ``` 輸出結果 : ``` apple ``` :warning: 另外,索引值也可以是負數,負數的意思是從後面數回來 ! 所以索引值-1的意思是倒數第一個元素,索引值-2的意思是倒數第二個元素,索引值-3的意思是倒署第三個元素...以此類推 範例如下: ```python= fruits=["apple","banana","grape","pineapple"] print(fruits[-2]) # 這行的意思是印出fruits串列中索引值-2的位置,也就是倒數第二個元素 ``` 輸出結果 : ``` grape ``` --- ## 串列的新增、修改和刪除 ### 1. 串列的新增 * ==append()方法== append()方法是從串列尾端新增一個元素進去 範例如下 : ```python= fruits=["apple","banana","grape","pineapple"] # 建立一個fruits串列 print(fruits) # 印出原本的fruits串列 fruits.append("mango") # 新增一個"mango"元素到fruits串列尾端 print(fruits) # 再印出新增元素後的fruits串列 ``` 輸出結果 : ```python ["apple","banana","grape","pineapple"] ["apple","banana","grape","pineapple","mango"] ``` * ==insert()方法== insert()方法是可以在串列的任一位置新增元素,用法是指定插入新元素的索引值位置和值即可 範例如下 : ```python= letters=["a","b","c","d"] # 建立一個letters串列 letters.insert(0,"e") # 將"e"元素插入letters串列中的第一個位置 print(letters) # 印出letters串列 ``` 輸出結果 : ```python ["e","a","b","c","d"] ``` ### 2. 串列的修改 要修改串列中的某個元素,必須先指定串列名稱和想要修改項目元素的索引值位置,再指定新的值進去即可 範例如下 : ```python= colors=["blue","red","green"] # 建立一個colors串列 print(colors) # 印出原本的colors串列 colors[2]="yellow" # 將colors串列中的第三個元素值更改成"yellow" print(colors) # 印出修改元素值過後的colors串列 ``` 輸出結果 : ```python ["blue","red","green"] ["blue","red","yellow"] ``` ### 3.串列的刪除 * ==del陳述句== 知道元素是在哪一個位置(索引值),就可以使用del陳述句來刪除元素 範例如下 : ```python= colors=["blue","red","green"] # 建立一個colors串列 print(colors) # 印出原本的colors串列 del colors[1] # 刪除colors串列中的第二個元素 print(colors) # 印出刪除元素後的colors串列 ``` 輸出結果 : ```python ["blue","red","green"] ["blue","green"] ``` * ==pop()方法== pop()方法是源自於"堆疊",中文解釋是彈出的意思,將串列想像成堆疊的樣子,而pop()方法就是把堆疊最頂端的東西彈出來,堆疊的最頂端是最後加進去的元素,也就是說pop()方法是刪除串列尾端的項目 範例如下: ```python= colors=["blue","red","green"] # 建立一個colors串列 colors.pop() # 從colors串列尾端彈出一個項目 colors.pop() # 再colors串列尾端彈出一個項目 print(colors) # 印出更改後的colors串列 ``` 輸出結果 : ```python ["blue"] # 因為從尾端彈出的兩個元素,所以只剩"blue" ``` 如果只是想要**暫時**刪除串列中的元素,我們可以將彈出來的值儲存到一個變數中 範例如下 : ```python= colors=["blue","red","green"] # 建立一個colors串列 value=colors.pop() # 將colors串列尾端彈出的一個項目存到value變數中 print(value) # 印出value值 ``` 輸出結果 : ```python green ``` :bulb: 如果知道元素的索引值,就可以任意pop()出想要的值 範例如下 : ```python= colors=["blue","red","green"] # 建立一個colors串列 value=colors.pop(1) # 將colors串列的第二個元素彈出然後存到value變數中 print(value) # 印出value值 ``` 輸出結果 : ```python ["blue","green"] ``` * ==remove()方法== 有時候我們不知道元素的索引值是多少但又想要刪除它的時候,我們可以使用remove()方法,它是一個可以依據值來刪除元素的方法 範例如下 : ```python= name=["Mary","John","Leo"] # 建立一個name串列 print(name) # 印出原本的name串列 name.remove("Leo") # 刪除name串列中的"Leo"元素 print(name) # 印出更改後的name串列 ``` 輸出結果 : ```python ["Mary","John","Leo"] ["Mary","John"] ``` 如果只是想要**暫時**刪除串列中的元素,我們可以先定義一個變數來儲存要刪除的值,然後再用remove()刪除它 範例如下 : ```python= name=["Mary","John","Leo"] # 建立一個name串列 value="Leo" #定義變數value是"Leo" name.remove(value) # 刪除name串列中的"Leo"元素 print(name) # 印出更改後的name串列 ``` 輸出結果 : ```python ["Mary","John"] ``` --- ### 串列的排序 * ==sort()方法== sort()方法可以讓串列按字母順序排序,但是是**永久性的改變排列順序** 範例如下 : ```python= letters=["b","c","d","a"] # 建立一個letters串列 letters.sort() # 將letters串列依字母順序進行排列 print(letters) # 印出排列過後的letters串列 ``` 輸出結果 : ```python ["a","b","c","d"] ``` 如果想要讓串列依字母相反順序排序的話,要在sort()方法中傳入reverse=True參數,範例如下 : ```python= letters=["b","c","d","a"] # 建立一個letters串列 letters.sort(reverse=True) # 將letters串列進行反序排列 print(letters) # 印出排列過後的letters串列 ``` 輸出結果 : ```python ["d","c","b","a"] ``` * ==sorted()方法== sorted()方法跟sort()方法很像,不過sorted()方法是可以**暫時性的改變排列順序**,不會影響原串列的排列順序 範例如下 : ```python= letters=["b","c","d","a"] # 建立一個letters串列 print(sorted(letters)) # 印出暫時依字母順序排列的letters串列 print(letters) # 再印出letters串列會發現它還是原本的letters串列 ``` 輸出結果 : ```python ["a","b","c","d"] ["b","c","d","a"] ``` 如果想要讓串列依字母相反順序排序的話,要在sorted()方法中傳入reverse=True參數,範例如下 : ```python= letters=["b","c","d","a"] # 建立一個letters串列 print(letters.sorted(reverse=True)) # 印出暫時依字母反序排列的letters串列 print(letters) # 再印出letters串列會發現它還是原本的letters串列 ``` 輸出結果 : ```python ["d","c","b","a"] ["b","c","d","a"] ``` * ==reverse()方法== reverse()方法是可以反轉串列原本的順序,但它也是**永久性的改變** 範例如下 : ```python= letters=["b","c","d","a"] # 建立一個letters串列 letters.reverse() # 把letters串列的順序反轉 print(letters) # 印出反轉後的letters串列 ``` 輸出結果 : ```python ["a","d","c","b"] ["b","c","d","a"] ``` #### 今天結束,各位明天見 :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