Eric
    • 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
    # Python學習筆記 >數字大於2^63取餘會爆炸 ## 基礎語法 ### I/O ```python= name=input("What is your name?") print("my_name","Eric") #會以空白隔開 => my_name eric print("a","b",sep="<->") #會以<->當作分隔 => a<->b print("a","b",end=' ') #以空格當結尾,預設是換行 => a b n, m = map(int, input().split(',', 2)) #輸入兩個用逗號隔開的整數 ``` ### Dictionary 大括號宣告,相當於C++的map ```python= dic={'a':1,'b':2,'c':4} print(dic['c']) #=>4 #print all keys for i in dic: print(i,end=' ') #print all values for i in dic.values(): print(i,end=' ') ``` ### Set 大括號宣告,不會有重複元素,會自動排序,跟C++一樣不可用索引值取元素 ```python= s=set() #建立空集合 s={i for i in range(5)} #s={0,1,2,3,4} s.add(5) #加入元素 s.remove(4) #刪除元素 print(s) #{0, 1, 2, 3, 5} if 2 in s: #判斷2是否在set理 =>True ``` ### List 中括號宣告 注意:在函式裡修改外面宣告的list時,本尊會被修改,也就是說函式呼叫時傳進去的便變數是list的話會是傳指標,但傳一般變數時是複製一個副本,無法更改到外面的本尊。 ```python= A=['a string',23,100.232,'o'] print(A[2]) #=>100.232 B=[[None for _ in range(10)] for _ in range(10)] #10x10 list ``` ### tuple 小括號宣告,不能被修改 ```python= t=(1,2,3) #or t=1,2,3 print(t[1]) #=>2 ``` ### If else 記得冒號,「且」是and不是&& ```python= a=5 if a<4: print("a<4") elif a>=4 and a<6: print("a>=4 且 a<6") else: print("a>=6") #=> a>=4 且 a<6 ``` ### Range 用來存區間整數 參數 : (起始,終點(不包括),間隔) ```python= r=range(5,10) #5,6,7,8,9 print(r[4]) #=>9 r2 = range(5, 50, 5) #5,10,15,20 print(r2[3]) #=>20 ``` ### Loop ```python= for i in range(2,10,3): #起始值,結束值,增加值 print(i,end=' ') #2 5 8 a=[3,1,2,3] for i in a: print(i,end=' ') #=>3 1 2 3 i=0 while(i<10): print(i,end=' ') i+=1 #=>0 1 2 3 4 5 6 7 8 9 ``` ### String split可將字串拆成陣列,該分隔字元會被刪除,拆解後封裝成list join可組合陣列,中間可輸入連接詞 ```python= s="hello world" arr=s.split('o') print(arr) #=>['hell', ' w', 'rld'] s2='---'.join(arr) print(s2) #=>hell--- w---rld ``` ### Others ```python= a=[6,3,2,1,3,2,5] print(len(a)) #7 print(max(a)) #6 print(a.count(3)) #3出現2次輸出2 a.insert(2,100) #在index=2的位置插入100 a.remove(2) #移除第一個2 a.reverse() #反轉 print(a) #[5, 2, 3, 1, 100, 3, 6] b=[1,2,3,4,5] del b[1:3] #刪除index1到index3前一個的所有元素 => b=[1, 4, 5] a.append(b) #將b塞到a的最後面 => a=[5, 2, 3, 1, 100, 3, 6, [1, 4, 5]] print(a[-1][2]) #-1代表最後一個元素, =>5 a.clear() #清空 c=[] for i in range(10): c.append(i) print(c) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] d=[i for i in range(10)] print(d) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] #只取小於5的奇數 e=[i for i in d if i<=5 and i&1] print(e) #[1, 3, 5] ``` ### Class __init__函式會在宣告此Class型別變數時自動執行初始化 __str__函式會在print時執行,返回一個字串當作print輸出的內容 ```python= class Student: def __init__(self,id,seat): self.id=id self.seat=seat def __str__(self): return f'My ID is {self.id}, and my seat number is {self.seat}.' def change_seat(self,newSeat): self.seat=newSeat student=Student("848392","3A2B") student.change_seat("8A7B") print(student) #My ID is 848392, and my seat number is 8A7B. ``` ### Numpy 用於進階數學運算 ```python= import numpy as np ``` #### 基本宣告 Zeros及ones為陣列元素的初始值,且括號裡的數字是大小,不是元素 ```python= a=np.array([1,2,3]) # 一維 b=np.array([[1,2,3],[4,5,6]]) #二維 c=np.zeros(5) #[0,0,0,0,0] d=np.zeros((2,3)) #2×3的陣列,初始化為0 e=np.ones((3,4,5)) #3×4×5的陣列,初始化為1 ``` #### 加減乘除 numpy的加減乘除為同一個元素位置的運算 一般list則為整個list運算(只有+號) ```python= a=np.array([1,2,3]) b=np.array([2,3,4]) print(a+b) #[3 5 7] c=[1,2,3] d=[2,3,4] print(c+d) #[1, 2, 3, 2, 3, 4] ``` #### 矩陣相乘 ```python= a=np.array([[1,2], [2,3]]) b=np.array([[3,4], [4,5]]) print(a.dot(b)) #[[11 14] # [18 23]] ``` #### 內積完相加 ```python= a = np.array([3, 5, 6]) b = np.array([2, 6, 1]) print(a @ b) #: 6+30+6=42 ``` ### 其他 ```python= a=[1,2,3,4,5] b=a[-1] #最後一個元素 c=a[::-1] #反向讀取 [5,4,3,2,1] d=a[3::-1] #從index=3開始反向讀取 [4,3,2,1] ``` ## 常用指令 ### 安裝函式庫 ``` pip install [name] ``` ### 查看python已安裝函式庫 ``` pip list ``` ### 安裝函式庫報錯 如果報錯顯示是在原碼中的語法錯誤,可以用以下方法解決: ![](https://i.imgur.com/2GC5jFB.png) #### Step1: 下載函式庫 首先到PyPI這個網站搜尋要下載的函示庫,進入頁面後點擊左側的Download files,把壓縮檔下載下來,接著解壓縮。 #### Step2: 更改錯誤 上面報錯內容為Setup.py檔案裡的40行少了一個括號,更改完後存檔。 #### Step3: 安裝 進入到該函式庫存放位置的跟目錄(預設為下載)並在路徑欄中輸入cmd進入終端,並用-e(從本機安裝)安裝該函式庫。 ``` pip install -e [file name] ``` 注意:這裡的file name是下載下來的資料夾名稱,不是該函式庫的名稱。 **接著就大功告成了!!!**

    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