chia you chen
    • 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
    • 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 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: Review tags: sprouts slideOptions: theme: solarized transition: 'fade' --- # Review 搭上複習快車ㄅ!!! --- # Input 篇 燃燒你的IO魂 ---- ## Mission 1 吃甚麼吐什麼 ---- ## Input & Output ```python= # Input hello sprout # Output hello sprout ``` ---- ## Answer ```python= word = input() print(word) ``` ---- ## Caution - input吃進來永遠是字串 ---- ## Mission 2 Eat an integer and print add 1 ---- ## Input and output ```python= #input 1 #output 2 ``` ---- ## Answer ```python= a = int(input()) print(a+1) ``` ---- ## Caution - Input進來會是字串,要用int強制轉型 ---- ## Mission 3 吃很多個整數,每個都加1 ---- ## Input & Output ```python= # Input 3 4 6 # Output 4 5 7 ``` ---- ## Answer ```python= # 吃進input然後以空白分割 # list of string nums_str = input().split() # 用map把裡面每個元素都轉型成int nums = list(map(int,nums_str)) ans = [] for i in nums: # 為了之後用join做準備,所以先轉型 ans.append(str(i+1)) print(" ".join(ans)) ``` ---- ## Caution - 別忘了split - 別忘了轉型 ---- ## 預測未來 ```python a = input("安安") ``` a = ? ---- ## Ans 使用者輸入r,"安安"只是問候語。 ![](https://i.imgur.com/on1NLPi.png) --- # Output篇 ---- ### print(1.1,"23",16,sep=".",end='') ---- ## sep 每筆資料中間銜接的部分。 ```python= print(1,2,3,4) # 1 2 3 4 print(1,2,3,4,sep="*") # 1*2*3*4 ``` ---- ## end 結尾的字元,default '\n' ```python= print("安安") print("Rilak") # 安安 # Rilak print("安安",end='r! ') print("Rilak") # 安安r! Rilak ``` ---- ## 練習 - 3057 : DEBUG 來印三角形吧 --- ## Boolean 布林 ---- ## True vs False ---- ## 運算 - and/or ![](https://i.imgur.com/1iZZJfc.png) ---- ## 運算 - not ```python= not False # True ``` ```python= not True # False ``` ---- ## 運算 - 比較 ```python= 1 > 3 # =>False 1 != 3 # => True 1 == 3 # => False 2 <= 3 # => True 9 > 3 > 2 >1 #=> True ``` ---- ## 運算 x 字串 x 外傳 ```python= "Sprout" == "忠毅" # False ``` ---- ## 運算 x 字串 x 意外 ```python= "123" > 12 ``` ---- ## Python2 ```python "123" > 12 # True ``` ## Python3 ```python "123" > 12 #Traceback (most recent call last): # File "<stdin>", line 1, in <module> #TypeError: unorderable types: str() > int() ``` --- # if 分枝篇 ---- ## if x 基礎 x 格式 ```python= if 條件1: # 做某些事 # 別忘記縮排 elif 條件2: # 若不符合條件1 # 但是符合條件2 # 做某些事 else: # 否則做某些事 ``` ---- ## if x 基礎 x 範例 ```python= Rilak_has_answer = True if Rilak_has_answer: print("Rilak快教教我r") else: print("Rilak快想想r") ``` ---- ## if x 基礎 x 解說 如果Rilak_has_answer,就輸出"Rilak快教教我r",不然就輸出"Rilak快想想r"。 ---- ## if x 變化 x 巢狀 ```python= if 條件: if 條件: #做某些事 else: #做某些事 else: #做某些事 ``` ---- ## if x 變化 x 範例 輸出1-50間所有是3的倍數但不是2的倍數的數字? ---- ## if x 巢狀 x 解法 ```python= for i in range(1, 51): if i % 3 == 0: if i % 2 != 0: print (i) ``` ---- ## if x 巢狀 x 醜 - and ```python= for i in range(1, 51): if i % 3 == 0 and i % 2 != 0: print (i) ``` --- # 迴圈篇 ---- ## 迴圈 x 基礎 x while 只要滿足條件,就繼續。 ```python= while 條件: # 做某件事 # 要記得縮排喔!!! ``` ---- ## 迴圈 x 基礎 x break 一個蘿蔔一個坑。 一個break一個迴圈。 ```python= n = 0 while True: if n > 3: break n+=1 ``` ---- ## 迴圈 x 進階 x break ```python= n = 0 while True: for i in range(5): if n > 10: break n+=3 n+=1 if n > 20: break print(n) ``` 問n = ? ---- ## 迴圈 x 解說 x break <iframe width="800" height="500" src="https://pythontutor.com/iframe-embed.html#code=n%20%3D%200%0Awhile%20True%3A%0A%20%20%20%20for%20i%20in%20range%285%29%3A%0A%20%20%20%20%20%20%20%20if%20n%20%3E%2010%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20break%0A%20%20%20%20%20%20%20%20n%2B%3D3%0A%20%20%20%20n%2B%3D1%0A%20%20%20%20if%20n%20%3E%2020%3A%0A%20%20%20%20%20%20%20%20break%0Aprint%28n%29&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=0&heapPrimitives=false&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe> ---- ## 迴圈 x 進階 x continue 有時,你會不想跑剩下的東西 ```python= for i in range(10): if i % 3 == 0: continue print(i) ``` --- # 迴圈篇 - 2 ---- ## 迴圈 x 基礎 x for ---- ## 迴圈 x for x Usage 配合range或是其他可以iter東西。 ---- ## 迴圈 x for x 例子 print 1 到 100 ---- ## 迴圈 x for x while解 ```python= k = 1 while k < 101: print(k) k += 1 ``` ---- ## 迴圈 x for x for解 ```python= for i in range(1,101): print(i) ``` ---- ## 迴圈 x range x 基礎 ```python= range(起始值,終止值,變化值) ``` ---- ## 迴圈 x range x 變異 ```python range(起始值,終止值) # 等價於 range(起始值,終止值, 1) ``` ---- ## 迴圈 x range x 孤單 ```python= range(中止值) # 等價於 range(0,終止值, 1) ``` ---- ## 迴圈 x range x 預測 ```python= print(list(range(9,3,5))) ``` --- ## List !!! 很重要!很重要!很重要! ---- ## List x 範例 x 製造 ```python= a = ['rilak',666] # a[0] 是 'rilak' # a[1] 是 666 ``` ---- ## List x 三寶 x len ```python= a = ['rilak',666] print(len(a)) # 2 ``` ---- ## List x 三寶 x 遍歷 b會依序迭代a裡的元素,第一次是'rilak'第二次是666。 ```python= a = ['rilak',666] for b in a: print(b) ``` ---- ### List x 三寶 x 遍歷 x 變異 ```python= a = ['rilak',666] for i in range(len(a)): print(a[i]) ``` ---- ### List x 三寶 x 雜 x min ```python= min([9,4,8,7]) # 4 ``` ---- ### List x 三寶 x 雜 x max ```python= max([9,4,8,7]) # 9 ``` ---- ### List x 三寶 x 雜 x sum ```python= sum([9,4,8,7]) # 28 ``` ---- ### List x 三寶 x 外傳 string也可以比min,max,按照字典序。 ```python= min(['a','b','c','d']) # 'a' ``` ---- ## List x 方法 x append ```python= a = [] print(a) # [] a.append(1) print(a) # [1] ``` ---- ## List x 方法 x pop ```python= a = [1,2,3,4] a.pop(1) print(a) # [1,3,4] ``` ---- ## List x 方法 x insert ```python= a = [1,2,3,4,5,6] a.insert(3,1000) print(a) # [1,2,3,1000,4,5,6] ``` ---- ## List x 方法 x sort ```python= a = [1, 2, 8, 7, 5, 6] a.sort() print(a) # [1,2,5,6,7,8] ``` ---- ## List x 方法 x reverse ```python= a = [1,2,8,7,5,6] a.reverse() print(a) # [6,5,7,8,2,1] ``` ---- ## List x 方法 x reverse 變異 ```python= a = [1,2,8,7,5,6] print(a[::-1]) ``` ---- ## List x 方法 x 比較 ```python= a = [1,2,8,7,5,6] print(a[::-1]) # [::-1]會製造一個新的list a = [1,2,8,7,5,6] print(a.reverse()) # a.reverse() 只會改變自己 # Nothing print(a) # [6,5,7,8,2,1] ``` --- ## 中場休息 - 3058 : ANIHC式排隊 - 3205 : 海捌樂愛的幸運數字 --- ## 字串 x 基礎 x 建立 ```python= a = "Sprouts" ``` ---- ## 字串 x 基礎 x list ```python= a = ['S','p','r','o','u','t','s'] # S a = "Sprouts" a[0] # S ``` ---- ## 字串 x 基礎 x 注意 immutable : 不能被修改 ```python= a = "123456" a[0] = "3" # Error ``` ---- ## 字串 x 三寶 x replace - replace(src, goal) - 因為string是immutable,replace回傳的會是新的string ```python= a = "hortune" print (a.replace("hortune", "rilak")) # rilak print (a) # hortune ``` ---- ## 字串 x 三寶 x split - split(sep,n) - n 代表要split幾個 ```python= a = "hortune rilak" k = a.split() print(a) # hortune rialk print(k) # ["hortune","rilak"] ``` ---- ## 字串 x 三寶 x find - find(target) - 尋找第一個符合target的位置 ```python= a = "hello moto" print(a.find("moto")) # 6 ``` ---- ## 練習 - 3059 : 黑人問號-問號黑人 --- # 字典篇 ---- ## dict x 基礎 x 建立 ```python= a = {"123" : 123, "bbb" : "ccc"} ``` ---- ## dict x 基礎 x 觀念 - 一個key對一個value - key要是immutable ---- ## dict x 基礎 x 插入 ```python= grade = {} grade["rilak"] = 666 ``` ---- ## dict x 基礎 x 遍歷 ```python= temp = {"a":1,"b":2} for i in temp: print(i,temp[i]) ``` ---- ## dict x 基礎 x 查詢 ```python= temp = {"a":1, "b":2} "a" in temp # True "c" in temp # False ``` ---- ## 練習 - 3208 : 海捌樂愛的大冒險 -- 異世界篇 --- # 函數篇 ---- ## 函數 x 基礎 x 定義 ```python= def hello(): pass # 別忘記縮排 ``` ---- ## 函數 x 基礎 x 參數 ```python= def c_sum(a,b,c): return a+b+c ``` ---- ## 函數 x 進階 x 參數 ```python= def pprint(a,b): print(a,b) pprint(1,2) # 1 2 pprint(b=1,a=2) # 1 2 ``` ---- ## 函數 x 變化 x 參數 ```python= def weird(q): q+=1 a = 1 weird(a) print(a) # 1 ``` ---- ## 函數 x 變化 x 參數 ```python= def weird(q): q.append(1) a = [] weird(a) print(a) # [1] ``` ---- ## 函數 x 變化 x 差異 - Immutable type - 傳入後不會影響原本的 - e.g. int, float, str - mutable type - 傳入後會影響原本的 - e.g. list, dict ---- ## 函數 x 變化 x 綜合 <iframe width="800" height="500" frameborder="0" src="https://pythontutor.com/iframe-embed.html#code=def%20qaq%28v_int,v_float,v_str,v_dict,v_list%29%3A%0A%20%20%20%20v_int%20%3D%201%0A%20%20%20%20v_float%20%3D%200.0%0A%20%20%20%20v_str%20%3D%20%22Lucky%22%0A%20%20%20%20v_dict%5B%22hi%22%5D%20%3D%200%0A%20%20%20%20v_list.append%28%22gogo%22%29%0A%0Av_int,%20v_float,%20v_str,%20v_dict,%20v_list%20%3D%200,%201.1,%20%22ykcuL%22,%20%7B%7D,%20%5B%5D%0Aqaq%28v_int,%20v_float,%20v_str,%20v_dict,%20v_list%29%0Aprint%28v_int,%20v_float,%20v_str,%20v_dict,%20v_list%29&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=10&heapPrimitives=false&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe> ---- ## 函數 x 參數 x default ```python= def weird(a = 1): print(a) weird(2) weird() ``` ---- ## 函數 x 參數 x default - default : 只能在後面 ```python= def weird(a=1,b): print(a) # SyntaxError: non-default argument follows default argument ``` ---- ## 函數 x 參數 x 未知 - 沒有指定名字的參數會被args收走 - 有指定名字會被kwargs收走 ```python= def weird(*args, **kwargs): print(args) print(kwargs) weird(6,6,6,name="rilak") # (6,6,6) # {"name":"rilak"} ``` ---- ## 函數 x 變數 x Scope - 訂在函數外的變數 - 大家都是全域變數 - 平安喜樂 ```python= b = 2 def q(): print(b) q() # 2 ``` ---- ## 函數 x 變數 x Scope - 函數裡的變數 - 只有函數裡能存取QQ ```python= def a(): q = 1 a() print(q) # NameError: name 'q' is not defined ``` ---- ## 函數 x 變數 x Scope - 這樣會如何呢? - 變數的scope在指定時確立 ```python= q = 2 def a(): q = 1 print(q) # 2 ``` ---- ## 函數 x 變數 x 怪異 - 因為python define時是以一個closure去確立變數關係 ```python= b = 2 def q(): print(b) b = 3 q() #Traceback (most recent call last): # File "<stdin>", line 3, in <module> # File "<stdin>", line 4, in q #UnboundLocalError: local variable 'b' referenced before assignment ``` ---- ## 函數 x 怪異 x 發想 - 想讓函數內改變global? - keyword : global ```python= b = 2 def q(): global b print(b) b = 3 q() print(b) # 3 # 3 ``` ---- ## 函數 x 區域 x 尋找 - locals ```python= print(locals()) #{'__spec__': None, '__builtins__': <module 'builtins' (built-in)>, '__loader__': <class #'_frozen_importlib.BuiltinImporter'>, '__package__': None, '__name__': '__main__', '__doc__': None} def qq(): a = 1 print(locals()) qq() # {'a':1} ``` ---- ## 函數 x 函數 x ? ```python= def a(): b = 5 print(locals()) def q(): c = 3 print(locals()) q() a() # {'b':5} # {'c':3} ``` --- # THE END

    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