NCUE-MECode
      • 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
    1
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- tags: 置頂,released, python --- <a rel="license" href="http://creativecommons.org/licenses/by-nc/3.0/tw/"><img alt="創用 CC 授權條款" style="border-width:0" src="https://i.creativecommons.org/l/by-nc/3.0/tw/88x31.png" /></a><br />本著作係採用<a rel="license" href="http://creativecommons.org/licenses/by-nc/3.0/tw/">創用 CC 姓名標示-非商業性 3.0 台灣 授權條款</a>授權. 在服用所有團隊內容前先看[這篇](/lwJ3tssyTpiFL8B3R8SUlw),~~不然 @yen0224 會打你屁屁~~,不遵守請立即離開,凡繼續觀看其他內容則視為已同意此申明條款 # py課程大綱及教學講義 ## 課程內容 * 0.1 python introdution * 1.1 py overview * 1.1.1 py程式碼特色 * 1.1.2 變數 * 1.1.3 算術運算子 * 1.1.4 邏輯運算子 * 1.2 print * print() * print 字串/變數 * 練習1 * 1.3 input * input() * input例題 * eval() * split() * input/output/eval/split例題 * 1.4 if/if...else/if...elif..else/巢狀if * if 例題 * if....else 例題 * if...elif...else 例題 * 巢狀if * if 實作 * 1.5 while * while 介紹 * while 例題 * while 練習題 * 1.6 for loop * for 介紹 * for 例題 * for 練習題 * 雙層 for * 例題 * 練習題 # 0.1 python introduction >Python是一種易於學習、功能強大且被廣泛使用的高階程式語言 Python設計哲學強調程式碼的可讀性與簡潔的語法,其優雅語法和動態類型使其成為大多數平台上許多領域程式編寫和快速應用程序開發時的理想程式語言。 因為Python的功能強大及應用範圍廣,讓全球知名企業、資料科學家及更領域的專家,紛紛投入開發,例如Wikipedia, Google , Yahoo!, CERN and NASA p.s 人工智慧、大數據常常運用到python ## 1.0 安裝python [Setup Python Environment](/wmztU6KDQEiO4Cuwxyo5qQ?edit) ## 1.1.1 py程式碼特色 * py程式碼架構 * 縮排(一個tab) ```py= for i in range(10): print(i) ``` * #為單行註解 ```py= #在井字號後加入註解,註解的文字不會被程式碼編譯 ``` * '''或"""為多行註解 ```py= '''夾在三個雙引號或單引號中間可以多行註解 換行後還是在註解''' ``` ## 1.1.2 變數 > * 我們可以使用指派運算子(=)(assignment operator) 來指派變數的值,而且是以最近一次指派的值為準 > * 可以不用宣告變數的型態 ## 1.1.3 算術運算子 ![](https://i.imgur.com/DKUSLxo.jpg) ## 1.1.4 邏輯運算子 ![](https://i.imgur.com/RbOBGFi.jpg) # 1.2 print() ## 1.2.1 print() > 我們可以使用py內建print()函式(是一個由py開發者事先寫好的程式),在螢幕上印出指定的字串 * 當要印出字串時,在字串前後加上兩個雙引號""或兩個單引號'' * 以逗號間隔想要印出的內容 * 在print()裡放入變數,程式編譯時會自動轉換成對應的值 * 在print()內輸入數字運算時,程式會自動運算,並印出結果 ```py= >>> print("I", "am", "Jenny") I am Jenny >>> x=5 >>> y=7 >>> print('x=',x,'y =',y) x= 5 y= 7 >>>print(10/5) 2 ``` ## 1.2.2 print()的預設值 * 當我們在使用print()語法時,程式執行時會預設sep=" "、end="/n",若我們想更改成其他值,我們可以使用以下語法。 ```py= >>> print("I", "am", "Jenny",sep=@,end="#") I@am@Jenny# ``` ## 1.2.3 py實作1 > 印出下列數學式結果 (1) 5*7+3*8 (2) 3-6/3+2 (也可以試試添加sep=,end=,熟悉此語法) # 1.3 input() ## 1.3.1 syntax >* 我們可以使用 Python 內建的 input 函式取得使用者輸入的資料 >* 括號內""中為選擇性參數,用來設定提示文字 >* 語法如下: >```py= >score=input("輸入成績") >print("成績為",score) >``` ## 1.3.2 eval() >* 我們可以使用Python內建的eval函式將括號內的字串轉換成數值 >* 語法如下: >```py= >score=eval(input("輸入成績")) >score=score+10 >print(score) >``` >:::warning >input:56 >::: >:::success >output:66 >::: >* 想想看如果不加eval()會output出甚麼? ## 1.3.3 split() >* 使用一些分隔符號來將一個字串分解成兩個以上字串 >* 可以在函式中指定分隔符號,若不指定分隔符號,split會使用所有的空白字元:換行符號、空格、及tab。 >範例程式: >```py= >[a,b,c]=input().split() >print(a) >print(b) >print(c) >``` >:::warning >input: >python is fabulous >::: >:::success >output: >python >is >fabulous >::: ## 1.3.4 實作 >(1) 寫一程式輸入三角形的底和高,求其面積 >Sample Input:5 10 >Sample Output:三角形面積為25 >(2) 寫一程式輸入班上5位同學成績,印出全班總分與平均。 >Sample Input:100 100 100 100 100 >Sample Output:全班總分為500分,平均為100 * 補充(自己看啦沒時間講了) ```py= >>> u = "www.doiido.com.cn" #使用默认分隔符 >>>print u.split() ['www.doiido.com.cn'] #以"."为分隔符 >>> print u.split('.') ['www', 'doiido', 'com', 'cn'] #分割0次 >>> print u.split('.',0) ['www.doiido.com.cn'] #分割一次 >>> print u.split('.',1) ['www', 'doiido.com.cn'] #分割两次 >>> print u.split('.',2) ['www', 'doiido', 'com.cn'] #分割两次,并取序列为1的项 >>> print u.split('.',2)[1] doiido (內容) ``` # 1.4 條件述句 ## 1.4.1 if...else.. * syntax: ```py= if condution: statement1(條件真實執行) else: statement2(條件假時執行) ``` * 範例程式 ```py= score =eval(input("請輸入數學分數")) if score>=60: print("及格") else: print("不及格") ``` ## 1.4.2 實作 >(1) 寫一程式,輸入X的值,依其實際值印出「X是基數」或「X是偶數」 >Sample Input:5 >Sample Output:X是基數 >(2) 寫一程式輸入X值,輸出Y值。若輸入的X值介於50到70(含)之間,將X的值指定給Y,否則Y的值為100。 >Sample Input:65 >Sample Output:65 ## 1.4.3 巢狀if (超過兩個以上情況) >* 範例程式 >```py= >score =eval(input("請輸入數學分數")) >if score>=90: > print("優等") >else: > if score >= 80: > print("甲等") > else: > if score>=70: > print("乙等") > else: > if score >= 60: > print("丙等") > else: > print("不及格") >``` >* if...elif...else >* syntax > ```py= >if condition 1: > statements1 >elif condition 2: > statements2 >elif condition 3: > statements3 >… >else: > statementsN+1 >``` >* 範例程式: >```py= >score =eval(input("請輸入數學分數")) >if score>=90: > print("優等") >elif score >= 80: > print("甲等") >elif score>=70: > print("乙等") >elif score >= 60: > print("丙等") >elif: > print("不及格") >``` >* 想想看為什麼甲等不用輸入<90的條件 ## 1.4.4 實作 >(1) 當兵條件 身高介於 150 到 190( 含 之間;體重介於 50 到 90( 含 間。 寫一程式輸入身高與體重,印出是否需要當兵。 # 1.5 while ## 1.5.1 while > 有時我們需要非一次性的東西,我們需要迴圈,而python最簡單的迴圈機制就是while。 > * syntax ```py= while condition1: statements1 #當符合條件condition1時執行statement1 ``` > * 範例程式: ```py= i= 0 while i<5: print(i) i= i + 1 ``` > * break > >如果你希望迴圈在某個情況時停止,但不確定那件事情甚麼時候發生,可以使用無窮迴圈和一個break陳述式。 >* 範例程式 > ```py= > while 1:#while 1為條件恆為真的意思 > stuff=input() > if stuff="quit": > break > print(stuff) ## 1.5.2 實作 >(1)寫一程式,持續輸入班上每一位同學的成績,若沒打成績直接enter時,代表成績輸入已結束。請印出全班的同學人數與成績總分。 >(2)猜密碼遊戲,寫一程式,當輸入值符合54624時,結束程式。 >Sample Input:54578 >Sample Output:錯誤請重新輸入密碼(不斷迴圈直至密碼正確) >Sample Input:54624 >Sample Output:答對了 # 1.6 for loop >## 1.6.1 for loop >for用來針對可迴圈(loop)的物件進行重複運算 >* syntax: >```py= >for i in range(start,stop,step): >#start:起始值 stop:停止值 step:每次變動值 >``` >* example: >```py= >for i in range (1,5,1): > print(i) :::success output: 1 2 3 4 ::: >```py= >for i in range (10,5,-1): > print(i) >``` :::success output: 10 9 8 7 6 ::: >## 1.6.2 for loop 的預設值(default) >當未指定時,預設start=0,step=1, >```py= >for i in range(8): > print(i,end=" ") :::success output: 0 1 2 3 4 5 6 7 ::: >```py= >for i in range(1,5): > print(i,end=" ") >``` :::success output: 1 2 3 4 ::: >## 1.6.3 for 例題 >(1) 運用迴圈計算求S S=2+4+6+8+……+30 >(2) 運用迴圈計算求S S=3 *9 *15 *21 >(3) 寫一程式,印出 1~100 間,所有 3 的倍數之值。 >## 1.6.4 雙層for迴圈 >* syntax >```py= >for i in range(1,10): > statements1 #s1 > for j in range(7,4,-1): > statements2 #s2 <img src=https://i.imgur.com/7UG0ivE.jpg width=300/> </br> > * example > 寫一程式 輸入全班 10 位同學的三次考試成績,求 (1)每位同學的平均分數。 (2)印出第一次的全班最高分 。 (3)第二次的不及格人數 ```py= max_1=0 fail_2=0 for i in range(1,11,1) sum=0 for j in range(1,4,1): score= eval(input('第'+str(i)+'位同學第+'str(j)+'次成績:')) if (j==1 and score>max_1): max_1=score if (j==2 and score<60): fail_2=fail_2+1 sum=sum+score print('==>第 str(i) 位同學平均為 :',sum/3) print("第一次最高分", max_1,"第二次不及格人數 :",fail_2) ``` 1.6.5 雙層for練習題 (1) S=1/3 + 3/7 + 5/11 + 7/15 (用單層for) (2) S=(3+5)+(3+5+7)+(3+5+7+9) -------------- # 參考資料 國立成大大學資訊工程學系陳培殷教授上課用PPT 還有一些網路上的參考資料,但現在找不太到在哪沒辦法列ಥ_ಥ

    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