臺中一中39th電研社教學
      • 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
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners 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
    • 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 Help
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
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners 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
    --- title: Python第一週講義 臺中一中電研社 slideOptions: theme: sky transition: 'convex' --- <style> .blue { color: #000080; } </style> <style> .silver { color: #c0c0c0; } </style> <font color="#000080">Python 第一週講義</font> === >[name= 林德恩、陳睿倬][time=Sep 24,2021] ###### tags:`python` `tcirc39th` `社課` `臺中一中電研社` --- ## <span class="blue">電研社</span> 社網:[tcirc.tw](https://tcirc.tw) online judge:[judge.tcirc.tw](https://judge.tcirc.tw) IG:[TCIRC_39th](https://www.instagram.com/tcirc_39th) --- ## <span class="blue">Python是什麼</span> Python是一種廣泛使用的直譯式、進階和通用的程式語言。Python支援多種程式設計範式,包括函數式、指令式、結構化、物件導向和反射式程式。它擁有動態型別系統和垃圾回收功能,能夠自動管理記憶體使用,並且其本身擁有一個巨大而廣泛的標準庫。...[維基百科](https://zh.wikipedia.org/wiki/Python) --- ## <span class="blue">使用python</span> 只要有安裝python就能使用python,使用python的最簡單的方法就是用**cmd**或**power shell**這種殼層軟體(**shell**)來執行,但一般來說都使用編譯器來執行。 本次為使用python3來教學。 --- ## <span class="blue">使用shell執行</span> ### 用shell簡略的執行程式 1. 開啟shell 2. 輸入python 3. 輸入程式碼(限一行) 4. 輸入**exit()**或**ctrl+z**來退出 ---- ### 用shell執行程式 1. 開啟shell 2. 用cd指令來進入到程式所在資料夾(可用dir指令輔助) ---- cd指令: * **cd 目標資料夾位置名稱** (變更目錄到指定資料夾) * **cd .\.** (回到上一層目錄) * **cd \\** (回到頂層) dir指令: * **dir** (查詢當前目錄) 切D槽/C槽指令 * **c** * **d** ---- **一個一個開** ``` C:\>cd users C:\Users> ``` **一次開** ``` C:\>cd \users C:\Users> ``` ---- 3. 輸入**python *程式名稱.py的程式*** 來執行 4. 可以使用**ctrl+c**來強制中斷 --- ## <span class="blue">開啟Python的IDE</span> python沒有IDE,只有IDE 可以編輯python的直譯器有很多,不過本次選用IDLE進行教學,因為它在APCS可使用,而且介面較簡單。 ---- ### 1. **開啟IDLE** ![](https://i.imgur.com/SEbWSTr.png) ---- ### 2. **按下File中的New File** ![](https://i.imgur.com/kT6xdm8.png) ---- ### 3. **再按下File中的Save** ![](https://i.imgur.com/lXCT1cZ.png) ---- ### 4. **為程式取名並選擇要存取的位置** ![](https://i.imgur.com/Oke7QEG.png) ---- ### 5. **等程式寫好時執行時,按下Run中的Run Module就會執行程式了** ![](https://i.imgur.com/ah6DdaG.png) --- ## <span class="blue">輸出(print)</span> 如果想要顯示出一些文字或數字這類資料的話,那其中最簡單的方法就是用print這個程式碼了。 * 輸出的python語法是print(變數)或print("文字") ※*python與其他語言不同的是""和''都表示字串* * print函式結尾符預設為換行字元"\n" * print的括號內可以用','分隔,但中間會空一格 ---- **程式** ``` print(變數1, 變數2...) print(字串1, 字串2...) ``` ```python= print(1) print("Hello world!") ``` **output** ``` 1 Hello world! ``` ---- **題目** * [b003: Hello,TCFSH!](https://judge.tcirc.tw/ShowProblem?problemid=b003) --- ## <span class="blue">跳脫字元</span> 在python中,只要不式從外部輸入的"\\",都有跳脫字元的功能,而跳脫字元就是將其後原本有特殊功能的字元轉成單純的字元,將部分普通字元轉成特殊功能的字元。 ---- 幾個常用的跳脫字元 \\\ = <span class="silver">\\</span> \\' = <span class="silver">'</span> \\" = <span class="silver">"</span> \\0 = <span class="silver">空字元</span> \\t = <span class="silver">Tab</span> \\n = <span class="silver">換行</span> \\b = <span class="silver">刪除前一個字元(在最後一個字不會起作用)</span> \\r = <span class="silver">將後面的文字覆蓋最前面的數個</span> \\a = <span class="silver">警示音</span> ---- \\加**三位數(可000)的8進位ascii碼** = <span class="silver">指定字元</span> \\x加**兩位數(可00)的16進位ascii碼** = <span class="silver">指定字元</span> \\u加**四位數(可0000)的16進位Unicode** = <span class="silver">指定字元</span> ---- **程式** ```python= print('\\','\'',"\0a\tb",'\n','\124','\x43','\u0046',"S H") print("123456","4567\rabcde") ``` **output** ``` \ ' a b T C F S H abcde6 4567 ``` ---- 題目 [b004: 串串的強迫症](https://judge.tcirc.tw/ShowProblem?problemid=b004) --- ## <span class="blue">設定變數(單一值)</span> 變數有很多種,它們都是用來儲存單一資料的容器。 變數基本種類如下: ---- 1. 布林值 (boolean) 如:True 2. 整數 (integer) 如:1 包含2進位、8進位、16進位整數 * 2進位要在數字前加0b * 8進位要在數字前加0o * 16進位要在數字前加0x ※輸出會轉10進制 3. 浮點數 (floating-point) 如:1.23 4. 文字 (string) 如:"Ans:1+1=2" ---- * 設定變數和更改變數的python語法都是 **變數名稱** = **資料** ※ *變數名稱不能取:**數字開頭**、**python保留的關鍵字(如print)**、**和自訂函式同名*** ---- **程式** ```python= word = "Hello world" print(word) number = 1 print(number) logic = True print(logic) ``` [範例程式碼](https://ideone.com/PeaQyb) **output** ``` Hello world 1 True ``` --- ## <span class="blue">進位制轉換</span> 如果要將10進位的數字轉成2進位/8進位/16進位顯示的話,可以用: 1. 2進位:bin(整數) 2. 8進位:oct(整數) 3. 16進位:hex(整數) ※轉換成文字變數 ---- **程式** ```python= a = 7 b = 9 c = 18 print("2進位:",bin(a)) print("8進位:",oct(b)) print("16進位:",hex(c)) ``` **output** ``` 2進位: 0b111 8進位: 0o11 16進位: 0x12 ``` --- ## <span class="blue">型別轉換</span> 如果要將資料型別轉換的話,可以用: 1. 轉成布林值:bool(整數/浮點數/文字) 2. 轉成整數:int(浮點數/布林值/文字(必須是純十進位整數的文字型別)) 3. 轉成浮點數:float(整數/布林值/文字(必須是純十進位數字的文字型別)) 4. 轉成文字:str(整數/浮點數/布林值) ---- **程式** ```python= a = 123 print("int ", a) a = float(a) print("float", a) a = int(a) print("int ", a) a = bool(a) print("bool ", a) ``` **output** ``` int 123 float 123.0 int 123 bool True ``` ---- 我們可以藉由type()函式來確認變數的資料型態 ```python= a = 1 b = 0.5 txt = "string" boolean = True print(type(a)) print(type(b)) print(type(txt)) print(type(boolean)) ``` **output** ``` <class 'int'> <class 'float'> <class 'str'> <class 'bool'> ``` --- ## <span class="blue">輸入(input)</span> 輸入的其中最簡單快速的一個方法就是input,它可以從鍵盤輸入資料進去變數。 * 設定變數的python語法是 **變數名稱** = input(**要再輸入前顯示的字串**) ---- **程式** ``` python= output = "NAME" output = input("What's your name? ") print("Hello", output) ``` **output** ``` What's your name? TSJ #輸入 TSJ Hello TSJ ``` ---- **題目** * [a001: 哈囉](https://judge.tcirc.tw/ShowProblem?problemid=a001) --- ## <span class="blue">基本運算子</span> 所有運算式同時也是資料,所以可以用在定義變數內的值或直接輸出,這次先教部分對人類來說較基礎的**算數運算子**和**字串編輯**的運算子。 ---- ### **\+** 用來執行加法運算 程式:**數值 + 數值** 如 1+1: ``` 1+1 ``` ---- **串接** 也可以用在**字串的連接** 程式:**文字 + 文字** 如 "1234554321": ``` "12345"+"54321" ``` 但注意,不能將文字型別跟其他的型別的變數相加,因為數值和文字的「+」不同,一個是相加,一個是串接,但可以將整數、浮點數跟邏輯互相運算。 ---- **錯誤範例** ``` 1+'1' ``` ---- ### **×** 用來執行乘法運算 程式:**數值 * 數值** 如 2×3: ``` 2*3 ``` ---- **重複文字** 程式:**文字 * 數值** 如 "aaaaa": ``` 'a'*5 ``` ---- ### **\-** 用來執行減法運算 程式:**數值 - 數值** 如 6-2: ``` 6-2 ``` ---- ### **/** 用來執行除法運算 程式:**數值 / 數值** 如 6÷3: ``` 6/3 ``` ---- ### **次方** **用來執行次方運算** 程式:**數值(底數) ** 數值(指數)** 如 2⁵: ``` 2**5 ``` ---- ### **取餘** **除法運算時取餘數** 程式:**數值 % 數值** 如 7÷5的餘數: ``` 7%5 ``` ---- ### **取整數商** **除法運算時的整數商** 程式:**數值 // 數值** 如 7÷5的整數商: ``` 7//5 ``` ---- ### **特殊運算規則** **()運算子** 括號內的數字會先運算 **先乘除後加減** 如果沒括號就優先進行先乘除後加減 ---- **程式** ```python= a = int(input("a:")) b = int(input("b:")) print("print a + b = ", a+b) print("print b - a = ", b-a) print("print a * b = ", b*a) print("print a / b = ", a/b) print("print a % b = ", a%b) print("print a ^ b = ", a**b) # a的b次方 print("print a / b = (取整數)", a//b) # a除以b取整數 ``` ---- **input** ``` a:2 b:3 ``` **output** ``` print a + b = 5 print b - a = 1 print a * b = 6 print a / b = 0.6666666666666666 print a % b = 2 print a ^ b = 8 print a / b = (取整數) 0 ``` ---- ### 儲存型別 如果是用變數儲存不同型別的計算結果的話,變數型別會更改,如:布林值+整數會變整數、整數×小數變小數,以此類推,如果將變數資料改成其他型別的資料,將會變成新的資料的資料型別。 ---- **程式** ```python= n = False print(n + 1) n = n + 2 print(n) print(n + 1.0) n = n +1.1 print(n) n = True print(n) n = n + 2.3 print(n) ``` ---- **output** ``` 1 2 3.0 3.1 True 3.3 ``` ---- ### **快速寫法** **變數+=數值**和**變數 = 變數 + 數值**相同 **變數 -=數值**和**變數 = 變數 - 數值**相同 **變數 \*=數值**和**變數 = 變數 * 數值**相同 **變數 /=數值**和**變數 = 變數 / 數值**相同 ---- [b006: 電電的梯形](https://judge.tcirc.tw/ShowProblem?problemid=b006) ※python 依目前所教的方法沒辦法同行輸入,所以沒辦法過,更改測試執行來執行。 ![](https://i.imgur.com/JnYjNa1.png) --- ## <span class="blue">註解</span> 在讀寫程式時,我們常會需要用到註解來標示此段程式碼在做什麼,以便之後寫程式時,或他人觀看你的程式碼時,能快速知道你的程式在做什麼。程式執行時不會執行註解的部分。 註解有兩種寫法 1. \# 註解....(只能用於單行) 2. """ 註解1 註解2... """(能用於多行) ---- ```python= print("hello") # 輸出 Hello """ 我是註解 """ ``` --- ## <span class="blue">錯誤(Error)</span> ### 語法錯誤 如果程式語法上有錯誤,那電腦就看不懂我們在寫什麼。 ```python= n = 0 n + 1 = n #沒有這種寫法 n++ #c/c++的語法 print(a) #沒設定變數a prin(n) #沒設定prin函式(之後會教自訂函式),也不是print() ``` ---- ### 語意錯誤 我們寫的程式和預期的不同,但語法正確,電腦會執行,不過結果和預期不同 如:要n但寫成m ```python= n = 1 m = 2 print(m) ``` **output** ``` 2 ``` 因為n寫成m,所以輸出2。

    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