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
    • 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 Note Insights 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

    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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    ###### tags: `python`,`資料型別` # Python 資料型別 最近更新日期:20201013 Python的五種基礎資料型別: 視為**資料儲存容器**。 * Number(數字): 有int(有號整數),long(沒用過),float(浮點數),complex(複數:例如5.2-2.5i 沒用過) * String(字串): 使用'或是" 包括起來的一串字元。 * List(串列) * Tuple(元組) * Dictionary(字典) 記憶順序為**序對(tuple)** 、 **串列[list]** 、 **{字典:dict}** 與 **集合{set}** 四種。 ###### 記憶方法: 由小到大 ()-> []-> {},最小的不能變更。 ## (序對,tuple) tuple用於依序儲存資料,可以依照順序取出資料,但不能更改,是不可變的物件; ## [串列,清單,list] 串列(list)用於依序儲存資料,可以依照順序取出,也可以更改。 ### 用法: ```python= myList = [1,2,'A','B'] #建立List(索引由0開始),1,2,'A','B' 為元素。 print(myList[2]) # A :用「[索引值]」讀取個別元素 # 用for迴圈,取出List的元素 for x in myList: print(x,type(x)) # 1 <class 'int'> # 2 <class 'int'> # A <class 'str'> # B <class 'str'> myList.count('A') # 回傳數值為 'A' 在 myList 中所出現的次數。 # 延伸引用: # 可以用此來判斷 元素是否存在於該list 中。 # (也可用下方index,但是若不存在在list中,會出現錯誤 ck = myList.count('C') # 不存在在myList中,則ck為0 index=myList.index('A') # 用「函式index」取出指定元素的索引值。 # list.index(x[, start[, end]]) 找不到,會丟出 ValueError 錯誤。 print('index=', index) # 2 print(len(myList)) # 4 用「len函式」計算List有多少元素。 List長度。 myList.append('D') # 追加資料。加入List資料。 print(myList) # [1, 2, 'A', 'B', 'D'] myList.insert(4,'C') # 用「函式insert」將元素插入到List的指定位置(第4索引的前面,第4個位置後面)。 print(myList) # [1, 2, 'A', 'B', 'C', 'D'] myList.extend(['a','b']) #將 iterable(可列舉物件)接到 list 的尾端。等同於 a[len(a):] = iterable。 print(myList) # [1, 2, 'A', 'B', 'a', 'b'] myList[len(myList):] = ['c','d'] # 串接list 方法二 print(myList) # [1, 2, 'A', 'B', 'a', 'b', 'c', 'd'] myList.remove('D') # 用「函式remove」將指定的元素從List中刪除 print(myList) # [1, 2, 'A', 'B', 'C'] del myList[3] # 用「函式del」將List中第幾個元素刪除。 print(myList) # [1, 2, 'A', 'C'] myList.clear() # 刪除 list 中所有項目。這等同於 del a[:] # 用「函式pop」將List中第幾個元素刪除,若不指定元素則刪除最後一個元素(採先進後出法)。 myList.pop(0) print('pop(0)',myList) # [2, 'A', 'C'] 有指定,由指定的位置刪除 myList.pop() print('pop()',myList) # [2, 'A'] 沒指定,由最後一個刪除。 myList.pop(-1) print('pop(-1)',myList) # [2] 負數,由右往左刪除。 # 排序:List中資料有數字與字串時,無法排序。(有無其他方法?) myList = ['C','D','Ab','Aa','Bb','Ba','AA','BB','A','B'] myList.sort() print(myList) # ['A', 'AA', 'Aa', 'Ab', 'B', 'BB', 'Ba', 'Bb', 'C', 'D'] myList = [1,2.3,2,8,9,10] myList.sort() print(myList) # [1, 2, 2.3, 8, 9, 10] # 反轉List (將原List反順序排列) myList = ['A','B',2,1] myList.reverse() # [1, 2, 'B', 'A'] print(myList) myList = [1,2.3,2,8,9,10] myList.reverse() print(myList) # [10, 9, 8, 2, 2.3, 1] # List轉為字串列方法:['a','b','c','d'] =>"a,b,c,d" # 方法一:需要元素均為字串,才有辦法使用此法 List=['a','b','c','d'] List2Str ='"'+','.join(List)+'"' print(List2Str) #"a,b,c,d" # 方法二:適用 文數字混和的List List=[1,'A','B',3] List2Str=str(List).replace('[','"',1).rstrip(']')+'"' print(List2Str) # "1, 'A', 'B', 3" List2Str='"'+str(List).lstrip('[').rstrip(']')+'"' print(List2Str) # "1, 'A', 'B', 3" ``` 參考:[Python 教學-5.資料結構](https://docs.python.org/zh-tw/3/tutorial/datastructures.html) ## {dict:字典} {key : value} 字典儲存資料方式為{鍵(key):值(value)}的對應方式。取出資料方式可使用「鍵」查詢「值」。 字典語法: dic = {key1 : value1, key2 : value2 } 範例: d1={} #空字典 運用: ```python= # 建立空字典方法: emptyDict = dict() emptyDict = {} # 建立字典,例如學生考試分數: studentScoreDict = {'Peter':80, 'Lowina':90, 'Alice':85} # 方法一 studentScoreDict = dict(Peter=80, Lowina=90, Alice=85) # 方法二 employeeDict = {83022:'Peter',10011:'Lowina'} # Key 也可以是數字 # 讀取資料方法 myDict = {'Peter':80, 'Lowina':90, 'Alice':85} print(myDict['Peter']) #80 若找不到值,會發生錯誤 print(myDict.get('Lowina')) #90 若找不到值,會傳回None # 新增或是修改 myDict['Peter']=70 # 更新資料 print(myDict['Peter']) # 70 myDict.update({'Peter':60}) # 字典內有,就更新 myDict.update({'Bossin':95}) # 字典內沒有,就新增 print(myDict) # {'Peter': 60, 'Lowina': 90, 'Alice': 85, 'Bossin': 95} myDictLen = len(myDict) # 字典項目數量(長度) del myDict('Bossin') # 刪除字典項目 del myDict # 刪除整個字典 # 字典迴圈處理範例 myDict = {'Peter':80, 'Lowina':90, 'Alice':85} for name in myDict: myDict[name] -= 10 print(myDict) # {'Peter': 70, 'Lowina': 80, 'Alice': 75} 每一位都減10分 # 用.items() 方法,每次迴圈回覆一個字組: myDict = {'Peter':80, 'Lowina':90, 'Alice':85} for name,value in myDict.items(): print(f'{name}:{value}, ', end='') # Peter:80, Lowina:90, Alice:85, for key in dict: print(key) # 列出key值 for key, value in dict.items(): print(key, value) #列出key與資料 ``` dict.clear() # 清空字典所有條目 del dict # 删除字典 參考:[第 12 章 字典](http://yltang.net/tutorial/python/12/) ## {集合} python的set集合,是一個無序不重複的元素集合。 ```python= ls =[1,2,1,3,'A','B','C','A'] ls2 = [1,3,4,5,'d','A',3,4,6] print(type(ls),len(ls),ls) # <class 'list'> 8 [1, 2, 1, 3, 'A', 'B', 'C', 'A'] st = set(ls) # 轉成集合 print(type(st),len(st),st) # <class 'set'> 6 {1, 2, 'B', 3, 'C', 'A'} st2 = set (ls2) # 轉成集合 print(type(st2),len(st2),st2) # <class 'set'> 7 {'A', 1, 3, 4, 5, 6, 'd'} print('交集',st & st2) # 交集 {'A', 1, 3} print('聯集',st | st2) # 聯集 {'A', 1, 2, 3, 'C', 4, 5, 6, 'd', 'B'} print('差集',st - st2) # 差集 {2, 'C', 'B'} 在st中,不在st2 中 print('差集',st2 - st ) # 差集 {'d', 4, 5, 6} 在st2中,不在st 中 ``` 參考:[python 集合比較(交集、並集,差集)](https://blog.csdn.net/isoleo/article/details/13000975) --- 參考:[Python 資料容器](https://sites.google.com/site/csjhmaker/q-python-xiang-guan/3-python-zi-liao-rong-qi) S20200609 By YTC M20200730,M20200814,M20200919,M20200929,M20201006 M20201013

    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