nowob
    • 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
    --- title: '' disqus: hackmd --- Python基本講義 === ## 前言 目前是為了資研社試教誕生的講義,沒意外的話下學期會持續更新直到我下幹,第一次編python講義有問題請鞭小力一點。 ## 如何開始自己的第一次Python 雖然很多人可能已經會了,但還是講一下避免有人不知道 ### 找到python在哪 假如你是在復旦的電腦教室,在搜尋欄打`idle`應該就找的到了 ![image](https://hackmd.io/_uploads/HJsk_OXMee.png) 長這樣版本可能會不一樣 ### 打開你的第一個檔案 點擊左上角的`file` ![image](https://hackmd.io/_uploads/B1xUudQGle.png) 點擊最上面的的`New File` ![image](https://hackmd.io/_uploads/B1Z_uO7fxl.png) 這樣子就完成寫python需要的前置作業了,接下來就開始實際操作吧 --- ## Hello, world ## 學程式的第一步當然是先試著跑出點簡單的東西 ```python= print("Hello, world!") ``` :::spoiler 為甚麼大家第一個程式都寫`Hello, world`呢 ? 可以參考維基百科 https://zh.wikipedia.org/zh-tw/Hello_World 簡單來說就是很久以前有人這樣寫,後面的人覺得很酷把他寫到自己的C教學書裡,就一路傳承到今天了 ::: `print()` 就是 Python 的輸出指令,字串要用引號包起來(單/雙引號都行)。 你想印什麼都可以塞進去,甚至可以直接印數字: ```python= print(114514) ``` 須注意這裡因為沒加符號電腦是判成單純數字 ```python= print("114514") ``` 假如今天這樣寫裡面的114514會被判成字串 ## 輸入 input() ## 要讓使用者輸入東西,用 `input()`。 這裡需要注意,直接做輸入電腦一律判成字串 ```python= a = input() print(a) # 你輸入什麼就輸出什麼 ``` 如果希望電腦判斷輸入的是數字或其他資料型態,可以這樣改 ```python= a = int(input()) ``` 通常題目會要你一次輸入好幾個數字,可以這樣寫: ```python= x, y = input().split() # 但這樣 x, y 都是字串 ``` 如果你要整數,一樣要再包一層 int: ```python= x, y = map(int, input().split()) ``` 沒看懂也沒關係,看到題目多練幾次熟起來自然也可以學會。 ## 多行輸入 有時候題目會給你很多行輸入,一直到沒有資料為止(像是 Judge 平台) ```python= while True: try: s = input() except EOFError: break ``` 這個寫法很常見,可以先記下來,之後會一直用到。 --- # 資料型態 # ## int(整數) ## 最基本的資料型態,只能存數字且不接受小數點, ```python= a = 5 b = 7 print(a + b) # 12 ``` python也有很多一般數學不會看到的運算子,像是: - `//` 整除 - `%` 取餘數 - `**` 次方 ```python= print(7 // 2) # 3 print(7 % 2) # 1 print(2 ** 5) # 32 ``` ## float(小數/浮點數) ## 有小數點就會自動是 float 型態: ```python= pi = 3.14 print(type(pi)) # <class 'float'> ``` ## str(字串) ## 所有用引號包起來的都是字串。 字串有很多內建功能,也很容易操作。 ```python= s = "konpeko" print(s[0]) # k print(s[-1]) # o print(s[1:4]) # onp ``` #### 把字串加起來 你可以直接「+」把字串連在一起,這是 Python 很厲害的地方。 ```python= a = "114514" b = "1919810" print(a + b) # 1145141919810 ``` 如果你想讓中間有空格,或是任何想要的字串,可以自己加: ```python= a = "Pekora" b = "cute" print(a + " " + b) # Pekora cute ``` ```python= a = "Pekora" b = "cute" print(a + " is " + b) # Pekora is cute ``` 或者直接 print 多個變數,Python 會自動幫你加空格: ```python= a = "maimai" b = "dx" print(a, b) # maimai dx ``` --- 格式化字串(f-string)可以在輸出的時候讓程式更好讀懂 ```python= name = "pekora" y = 3 print(f"{name} 是 {y} 期生成員") # pekora 是 3 期生成員 ``` 還有一些酷酷的內建函式可以用: ```python= s = "TEmPTaTiON" print(s.lower()) # temptation 全小寫 print(s.upper()) # TEMPTATION 全大寫 print(s.count('T')) # 3 反正就找幾個你指定的字元 print(len(s)) # 10 多長 ``` # 常用容器 # ## list(串列/陣列) ## 好用陣列,啥都能裝 ```python= lst = [1, 4, 5, 1] lst.append(4) # 加東西 print(lst) # [1, 4, 5, 1, 4] print(lst[0]) # 1 lst.pop() # 把最後一個拔掉 print(lst) # [1, 4, 5, 1] ``` 遍歷、切片、排序都很簡單: ## dict(字典) ## 類似C++的map,可以用唯一的key值對value,拿來查東西很方便 ```python= score = {"peko": 100, "miko": 90} score["peko"] += 10 print(score) # {'peko': 110, 'miko': 90} for name in score: print(name, score[name]) #peko 110 #miko 90 ``` ## tuple(元組) ## 跟 list 很像,但不能改內容(比較少用)。 ```python= t = (1, 2, 3) print(t[0]) # 1 ``` ## set(集合) ## 用來存「不重複」的東西,像數學裡的集合。 存到一樣的東西會去重自己刪掉 ```python= s = set() s.add(3) s.add(3) print(s) # {3} s.remove(3) ``` ---

    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