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[2] ###### [https://python-slide.macs1207.dev](https://python-slide.macs1207.dev) ###### 2019/10/22 ###### tags: `NKUST ITC` `Python 社課` [TOC] --- ## [Github](https://github.com/macs1207/python-tutorial) ---- ## 今天進度0% - 如果我單身,則我沒女友 - Only Loooooop - Loooooop in Loooooop - Function --- ## 如果我是單身,則我沒有女朋友 - Decision Making 做決策的方式,俗稱判斷式 ---- ### if & else ```python= status = True if (status): print("ITC") else: print("NOT ITC") ``` ---- [OREO](https://i.imgur.com/LO4kFto.png) > [圖源](https://www.reddit.com/r/Animemes/comments/abj521/wouldnt_wanna_eat_100_of_these_bad_boys/) ---- ### 如果這樣寫 ```python= re = 5 if re >= 5: print("OREREREREREO") if re >= 4: print("OREREREREO") if re >= 3: print("OREREREO") if re >= 2: print("OREREO") else: print("OREO") ``` <span> ?_? 這不是我要的結果R <!-- .element: class="fragment" data-fragment-index="1" --></span> ---- ### else if不能用? - 在Python裡這樣寫 -> elif ```python= e = 5 if e >= 5: print("OREREREREREO") elif e >= 4: print("OREREREREO") elif e >= 3: print("OREREREO") elif e >= 2: print("OREREO") else: print("OREO") ``` --- ## Only Loooooop - while - for ---- ### while - 未知執行次數 ```python= while 判斷式: do something ``` ### for - 已知執行次數 ```python= for i in range(初始值, 終止值, 更新值): do something ``` ---- ### 1.基本操作 - while ```python= while True: print("ITC!!!") ``` - for ```python= for i in range(0, 10, 1): print(i) for i in range(10): print(i) ``` ---- ### 2.檔案操作法 - for "for" ```python= f = open(filename, "r") for i in f: print(i) # 逐行讀取 ``` ---- ### Loop V.S. Decision Making - 如果達成條件 - Loop會不斷執行 - Decision Making只執行一次 - Like This ```python= status = True if status: print("!!!") else: print("???") while status: print("...") ``` ---- ### How to solve? - 使條件不成立 - Like This ```python= status = True while(status): print("NKUST ITC") status = False ``` - or insert if-statement to loop ```python= status = True while(status): print("NKUST ITC") if(status): break ``` ---- ### Wait...what is break??? - 控制流程ㄉ好幫手 - 「break、continue、pass」以上都是好幫手 - 而他們使用恰當是具有相當大的好處滴 - break:跳出或中斷 - continue:跳過且繼續 - pass:Nothing Happen ---- ### 484太難懂ㄌ? - 我們直接來看個範例好ㄌ ```python= for i in range(10): print(i) for i in range(10): if (i == 8): break print(i) ``` ---- ### continue怎麼用 ```python= for i in range(10): print(i) for i in range(10): if (i == 8): continue print(i) ``` ---- ### 前面提到pass,那是啥 - 佔位仔,舉例 1. 修了一堂人很多的課,太晚到會沒位置。 2. 進教室看到一個空位 3. 正要坐下去時,旁邊的人說:同學,這裡有人坐喔 4. 我:????? <span> - 這種人早晚會被打 <!-- .element: class="fragment" data-fragment-index="1" --></span> ---- - 重來一次 1. 修了一堂人很多的課,太晚到會沒位置。 2. 進教室看到所有座位都放了東西但沒坐人 3. \*\*沒有人\*\*: 4. 我:直接回家 - 此時,座位上放著的東西就是pass,單純用來佔位子 ---- ### 怎麼用呢 ```python= sleep = True if sleep: print("zzzzz") else: pass # 醒著也什麼都不做 ``` --- ## Loop in Loop ---- ### What is loop in loop - 當一個迴圈需要第二個 第三個 ... 第N個的時候 - 蛤?你在工三小? - 算ㄌ...讓我們直接看ㄍ範例 ---- ### (應該不是)金字塔三角形 - 當今天要印出「5x5的金字塔」,你會怎麼寫? - 長得像這個鳥樣 ![](https://i.imgur.com/wLxLnUp.png) ---- - 寫法1 ```python= print(" *") print(" **") print(" ***") print(" ****") print("*****") ``` ---- - 寫法2 ```python= counter = 4 for i in range(5): if i < counter: print(" ", end='') else: print("*", end='') counter -= 1 print() for i in range(5): if i < counter: print(" ", end='') else: print("*", end='') # ... 同上,以下省略 ``` - 以上的寫法我想到頭很痛 ---- - 寫法3 ```python= layer = int(input("你要幾層的三角形ㄋ:")) for i in range(layer): for j in range(layer - (i + 1)): print(" ", end='') for j in range(i + 1): print("*", end='') print() ``` ---- - 給新手ㄉ練習題目 - 讀檔輸入,輸出對應數字的圖形 - 像這樣 ``` # 輸入檔案:指定圖形 行寬或列高 & 指定圖形: 1:方形 2:三角形 3:菱形 # File:1 3 # 2 3 # 3 5 *** *** *** * *** ***** * *** ***** *** * ``` ---- ### 有人想知道這個怎麼印 ``` * *** ***** ``` - 這樣印 ```python= layer = int(input("你要幾層ㄋ:")) for i in range(layer): for j in range(layer - i - 1): print(" ", end='') for j in range(2 * i + 1): print("*", end='') print() ``` ---- - 練習題答案在[Github CH3](https://github.com/chrisliu430/python-tutorial/tree/master/ch3/for_in_for.py)喔 --- ## Function(函式) ---- ### 為什麼需要這個,我討厭數學QQ - 提高程式碼的可重用性 - 方便維護 - 程式碼簡潔 ---- > 你說簡潔就簡潔喔 > [name=沒有人] ---- ## 舉例 - 又是BMI ```python= def bmi(height, weight): bmi = weight / ((height / 100) ** 2) return "BMI是: {}".format(bmi) people = [ { "height": 180, "weight": 90 }, { "height": 120, "weight": 20 } ] for i in range(len(people)): print(bmi(people[i]["height"], people[i]["weight"])) ``` ---- - 在Python中,不須指定函式的回傳型別 - 不回傳的話,不寫return就行 ---- ### 練習 - 輸入要做什麼事,執行指定的函式 - 沒有設計動作的話,輸出"我不會"之類的東西 - 像這樣 ``` 請輸入動作: 說早安 輸出: 早安 請輸入動作: 睡覺 輸出: 我不會QQ ``` - 提示: 函式也可以存在dict或list中 ---- - 練習題答案在[Github CH3](https://github.com/macs1207/python-tutorial/blob/master/ch3/action_example.py)喔 --- ## 專案建議??? - 沒有想到可以做甚麼ㄉ,可以參考以下 - Discord Bot 狼人殺 - 可串Dialogflow - 高科美食地圖 - 高科聊天室(參考Dcard抽卡) ---- ### 接續ㄏ - 打磚塊 - 自動打磚塊也能做 - 俄羅斯方塊 - 冷氣遙控器 ---- ### 消夜文 ###### 一碗濃湯,美味的濃湯,香甜可口,一碗只要25,心動嗎?來買呀! ![](https://i.imgur.com/qGGklEs.jpg) --- ## Discord - [https://discord.gg/hmUeXeH](https://discord.gg/hmUeXeH) ![](https://i.imgur.com/Ev2zJUe.png) ---- ## Telegram - [https://t.me/kuasitc](https://t.me/kuasitc) ![](https://i.imgur.com/mYOQ7nT.png) ---- ## 作者 - 社長 [Macs](https://github.com/macs1207) - 總務 [C.H](https://github.com/chrisliu430) ---- ## 參考資料 - [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