Macs Lin
    • 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
    # NKUST ITC 108-1 Python[0] ###### [https://python-slide.macs1207.dev](https://python-slide.macs1207.dev) ###### 2019/10/08 ###### tags: `NKUST ITC` `Python 社課` [TOC] ---- ## [Github](https://github.com/macs1207/python-tutorial) --- ## 社課模式 - 專案實作 - 邊做邊學效率hen好 - 分組進行 - 3~5個人左右 - 已經有想好專案題目的人可以告訴我 - 可以幫你宣傳拉人一組(? - 沒有的話,請和你周圍的好同學先組好一組 - 互相扶持,互相學習 - 可以開始討論專案題目! ---- ## 分組時間 ### 分組分組分組分組分組分組分組分組分組分組分組分組分組分組分組分組分組分組分組分組分組分組分組分組分組分組分組分組分組分組分組分組分組分組 --- ## 超理想社課進度表 |日期|活動內容| |:--:|:-----:| |10/08|環境建置、資料型態跟一些有的沒的| |10/15|json、import、讀寫檔| |10/22|判斷式結構、迴圈、函式| |10/29|預備週| |11/05|考試週| |11/12|封包、requests、API、DOM解析| |11/19|以下待定| --- ## Python是什麼r 我都念Python - 維基百科如是說 >Python是一種廣泛使用的直譯式、進階編程、通用型程式語言,由吉多·范羅蘇姆創造,第一版釋出於1991年。可以視之為一種改良的LISP。 ---- ## 簡單地說 <span><li>直譯式語言</li><!-- .element: class="fragment" data-fragment-index="1" --></span> <span><li>資料科學與ML的好朋友</li><!-- .element: class="fragment" data-fragment-index="2" --></span> <span><li>這學期社課使用的工具</li><!-- .element: class="fragment" data-fragment-index="3" --></span> --- ## 環境建置 ---- ## 檢查是否安裝Python - 請打開Command Line,輸入這行 ``` python -V ``` - 假如出現 <span><h4>Python 3.X.X</h4><!-- .element: class="fragment" data-fragment-index="1" --></span> <span><h3>那就是裝好啦</h3><!-- .element: class="fragment" data-fragment-index="2" --></span> ---- ## 假如沒有? ---- ## 1. 安裝Python ---- ### 把"Add Python 3.7 to PATH"打勾 <span>![](https://i.imgur.com/atGd36K.png)<!-- .element: class="fragment" data-fragment-index="1" --></span> ---- ### 按一下Install Now ---- ### 再按一下這個 <span>![](https://i.imgur.com/nhUccf5.png)<!-- .element: class="fragment" data-fragment-index="1" --></span> <span><h3>然後Close</h3><!-- .element: class="fragment" data-fragment-index="2" --></span> ---- - 請再打開Command Line,輸入這行 ``` python -V ``` - 應該出現 <span><h4>Python 3.X.X</h4><!-- .element: class="fragment" data-fragment-index="1" --></span> ---- - 一樣是Command Line,輸入這行 ``` pip -V ``` - 假如沒有這個 <span><h4>pip xx.x.x from ...</h4><!-- .element: class="fragment" data-fragment-index="1" --></span> <span><h3>請找助教幫忙ㄛㄛㄛ:weary:</h3><!-- .element: class="fragment" data-fragment-index="2" --></span> --- ## 資料型態 ---- ## 簡單講物件(Object) - 物件可以被理解為一種容器 - 每個物件有屬於自己的型別(type)與值(value) ---- - 舉個例子 |型別(type)|值(value)| |:-------:|:-------:| |int|1, 0, -1, -2| |float|1.0, 1.5, -2.3| |string|"Hello world!"| |list|[1, 2, "早安", 3.0]| |dict|{"key": "value", 1: 234}| |bool|True, False| ---- ## 變數(Variable) - 在Python中,不用宣告型別(int、float) ---- - 強型別程式語言像是C之類的 - ==int== year = 9012; <span> - 像是Java - ==String== name = "ITCCC"; <!-- .element: class="fragment" data-fragment-index="1" --></span> <span> - 在Python中,這些都不重要啦(??? - year = 9012 - name = "ITCCC" - cpu = {"AMD": "真香"} - amd = True <!-- .element: class="fragment" data-fragment-index="2" --></span> ---- - 變數的有效命名規則 1. 開頭必為大小寫英文字母或底線 2. 非開頭可以為大小寫英文字母、數字、底線 3. 區分大小寫(case sensitivity) 4. 除非你討厭人類,不然取個有意義的變數名稱 ---- - 做個小規範 1. 變數名稱 - variable_name 2. 常數變數名稱 - CONSTANT_NAME 3. 函式、方法名稱 - function_name, method_name 4. 類別名稱 - ClassName ---- ## 這個很重要 會考 - 變數名稱不能是保留字,Python的保留字有 ![](https://i.imgur.com/FTcDwqr.png) --- ## 輸入與輸出 #### ㄜ 那是啥 ---- ## 輸出是 <span><h3>電腦顯示給你看(嗎)</h3><!-- .element: class="fragment" data-fragment-index="1" --></span> <span><h2>那~~輸贏~~輸入?</h2><!-- .element: class="fragment" data-fragment-index="2" --></span> <span><h2>你給電腦的一切。</h2><!-- .element: class="fragment" data-fragment-index="3" --></span> ---- ## 怎麼寫呢? ```python= something_ghost = input("輸入一些什麼鬼都行:") print(something_ghost) ``` <span><h5>輸入一些什麼鬼都行:123 123</h5><!-- .element: class="fragment" data-fragment-index="1" --></span> ---- ## 不管輸入什麼,input()的回傳值必為String <span><h3>不要瞎掰好嗎</h3><!-- .element: class="fragment" data-fragment-index="1" --></span> <span> ```python= type(something_ghost) ``` <!-- .element: class="fragment" data-fragment-index="2" --></span> <span><h5>\<class 'str'\></h5><!-- .element: class="fragment" data-fragment-index="3" --></span> ---- ## 怎麼解決呢? <span> ```python= a = int(something_ghost) type(a) ``` <!-- .element: class="fragment" data-fragment-index="1" --></span> <span><h5>\<class 'int'\></h5><!-- .element: class="fragment" data-fragment-index="2" --></span> ---- ## 進階一點的話 ```python= print("高", "大", sep='科', end='\n資研社') ``` <span><h5>這樣會輸出:</br>高科大</br>資研社</h5><!-- .element: class="fragment" data-fragment-index="1" --></span> ---- ## WHYYYY? <span> <h3>print的參數</h3> <!-- .element: class="fragment" data-fragment-index="1" --></span> <span> - sep = ' ' - 指定每個輸出值之間的分隔字元 - 預設為空白 - end = ' ' - 指定在句尾輸出的最後加上字元 - 預設為換行 <!-- .element: class="fragment" data-fragment-index="2" --></span> --- ## 一堆運算子 ---- - 數學運算子 |運算子(operator)|這啥|範例|結果| |:-------------:|:-:|:-:|:-:| |+|加法|3 + 2|5| |-|減法|3 - 2|1| |\*|乘法|3 \* 2|6| |/|除法|3 / 2|1.5| |%|除法取餘數|3 % 2|1| |//|除法商取整數|3 // 2|1| |\*\*|次方|3 ** 2|9| ---- - 邏輯運算子 |運算子(operator)|這啥|範例|結果| |:-------------:|:-:|:-:|:-:| |or| 或 |(2 > 1) or (1 < 0)|True| |and| 及 |(2 > 1) and (1 < 0)|False| |not| 非 |not (1 < 0)|True| ---- - 比較運算子 |運算子(operator)|這啥|範例|結果| |:-------------:|:-:|:-:|:-:| |>|大於|3 > 2|True| |<|小於|-1 < 2.5|True| |==|等於|3 == 2.99|False| |>=|大於等於|1 >= 1.0|True| |<=|小於等於|2 <= -9|False| |!=|不等於|3 != 2.99|True| ---- ## 兩種不等於 - != - 比較值 - is not - 比較物件 ```python= 1 == 1 # -> True 1 != 1 # -> False [] is [] # -> False (不同物件) [] is not [] # -> True a = b = [] a is b # -> True (相同物件) a is not b # -> False ``` ---- - 位元運算子(2進位) - 先跳過好了 ---- ## 再回到Python的資料型態 --- ## 1. List ---- - 有順序的物件集合,具有索引特性,長度可變動 ```python= empty = [] banknote = [100, 200, 500, 1000] name = ['銀色的鮮奶', '橙色的鮮奶', '綠色的鮮奶'] mixed = [1, 0.9, 'cat', [1,2], 'fruit'] print (empty, banknote, name, mixed) ``` <span><h5> [] [100, 200, 500, 1000] ['銀色的鮮奶', '橙色的鮮奶', '綠色的鮮奶'] [1, 0.9, 'cat', [1,2], fruit] [1, 0.9, 'cat', [1,2], fruit] </h5><!-- .element: class="fragment" data-fragment-index="1" --></span> ---- - 索引值從0開始 ![](https://i.imgur.com/pMznmGr.png) ---- |運算|描述| |:-:|:-:| |s[i] = x|將第i個物件換成X| |s[i:j]|選取s的第i個到第j-1個物件| |del s[i:j]|刪除s的第i個到第j-1個物件| |s[i:j:k]|選取s的第i個到第j-1且間隔為k的物件| ---- |運算|描述| |:-:|:-:| |x in s|物件x是不是在s中,若是為True,否為False| |x not in s|物件x不在s中,若是為True,否為False| |s + t|將s與t接起來| |len(s)|s的物件個數| |min(s)|s的最小值| |max(s)|s的最大值| ---- ## 經常搭配的方法 #### 要用再查就好 ---- |函式名|描述| |:---:|:-:| |append(object)|在末端加入物件| |insert(index, object)|依指定索引值插入物件| |pop(index)|從list移除物件(預設為最後一個)| |index(object)|找出物件的索引值| ---- |函式名|描述| |:---:|:-:| |remove(object)|移除list中的第一個物件| |reverse()|將list中的所有物件反向| |sort()|將list中的物件做排序| |count(object)|計算物件在list中出現的次數| |clear()|將list清空| ---- ### 用法像這樣 ```python= a = [1, 2, 3] a.append(4) ``` <span><h5> [1, 2, 3, 4] </h5><!-- .element: class="fragment" data-fragment-index="1" --></span> --- ## 2. Tuple ---- - 就像是list,不過list賦值之後可以更改,但是tuple不行。 ```python= name = ('Jonathan', 'Joseph', 'Jotaro', 'Josuke', 'Giorno' ) print(name) ``` <span><h5>('Jonathan', 'Joseph', 'Jotaro', 'Josuke', 'Giorno' )</h5><!-- .element: class="fragment" data-fragment-index="1" --></span> ```python= name = ('Jonathan', 'Joseph', 'Jotaro', 'Josuke', 'Giorno' ) name = name + ('Jolyne',) print(name) ``` <span><h5>('Jonathan', 'Joseph', 'Jotaro', 'Josuke', 'Giorno', 'Jolyne' )</h5><!-- .element: class="fragment" data-fragment-index="2" --></span> ---- 為什麼要加逗號:confused: ```python= name = ('Jonathan', 'Joseph', 'Jotaro', 'Josuke', 'Giorno', 'Jolyne' ) name = name + ('Jolyne') print(name) ``` <span><h5>TypeError: can only concatenate tuple (not "str") to tuple</h5><!-- .element: class="fragment" data-fragment-index="1" --></span> ---- - **List只有一個元素,不用加逗號就會視為List** - **Tuple只有一個元素,加逗號才會視為Tuple** - **切記!有更動到值的運算都不能使用在Tuple上** --- ## 3. Dict(字典) ---- - 宣告方式 ```python= grade = { 'Chinese': 98, 'English': 65, } ``` ---- - Dict與List的差異 ![](https://i.imgur.com/dBDsdpw.png) ---- - 取得資料的便利性 ```python subject = ['Chinese', 'English', 'Math', 'History', 'Physics', 'Chemistry'] score = [98, 65, 87, 77, 50, 88] score_index = subject.index('Physics') print("Physics score =", score[score_index]) ``` <span><h5>Physics score = 50</h5><!-- .element: class="fragment" data-fragment-index="1" --></span> ---- ```python= grade = { 'Chinese': 98, 'English': 65, 'Math': 87, 'History': 77, 'Physics': 50, 'Chemistry': 88 } print("Physics score =", grade['Physics']) ``` <span><h5>Physics score = 50</h5><!-- .element: class="fragment" data-fragment-index="1" --></span> ---- - 新增與修改 ```python= grade['Math'] = 100 print(grade) ``` <span><h5>{'Chinese': 98, 'English': 65, 'Math': 100, 'History': 77, 'Physics': 50, 'Chemistry': 88}</h5><!-- .element: class="fragment" data-fragment-index="1" --></span> ```python= grade['English'] = 95 print(grade) ``` <span><h5>{'Chinese': 98, 'English': 95, 'Math': 100, 'History': 77, 'Physics': 50, 'Chemistry': 88}</h5><!-- .element: class="fragment" data-fragment-index="2" --></span> ---- - Key與Value可以為任意型態(字串、數值) ```python= a = { 0: 'hi!', 'Apple': 12.3, } print(a) print(a['Apple']) ``` <span><h5>{0: 'hi!', 'Apple': 12.3} 12.3</h5><!-- .element: class="fragment" data-fragment-index="1" --></span> ---- ## 經常搭配的方法 #### 也是要用再查就行 ![](https://i.imgur.com/rWQ2QbJ.png) --- ## 4. 字串(String) ![](https://i.imgur.com/Pg1sKQD.png) ---- - 示範一下 ```python= str = "road roller" print(str) print(str[1]) ``` <span><h5>road roller o</h5><!-- .element: class="fragment" data-fragment-index="1" --></span> --- ## Discord - [https://discord.gg/hmUeXeH](https://discord.gg/hmUeXeH) ## Telegram - [https://t.me/kuasitc](https://t.me/kuasitc) ---- ## 作者 - 社長 [Macs](https://git.macs1207.dev) - 副社長 呆呆大蛙 ---- ## 參考資料 - [Python-100-Days](https://github.com/jackfrued/Python-100-Days) - [Wikipedia](https://zh.wikipedia.org) - [成大X-Village教材](https://www.facebook.com/pages/category/Education/X-Village-423736361424301)

    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