中興大學資訊社
      • 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 教學 description: 中興大學資訊研究社1091學期程式分享會主題社課 tags: Python --- #### meet.google.com/czn-pbav-zvr ### md.nchuit.cc/py/ # 回顧複習 > [name=VJ][time= 109,12,1] > [name=Hui][time= 110,11,25] --- ## 資料型態與轉型 ###### *詳細參閱: [Python內建資料型態](https://docs.python.org/zh-tw/3/library/stdtypes.html)* | `int` | `float` | `str` | `bool` | | ----- | ------- | ----- | ------ | | 整數 | 浮點數 | 字串 | 布林值 | | `list` | `dict` | `tuple` | `set` | | ------ | ------ | ------- | ----- | | 串列 | 字典 | 元組 | 集合 | --- ```python= a = 100 b = True c = 'word' #注意 Python 的字串用''或""都可以 d = [-1,False,c] e = {'mon':'mother','dad':'father'} f = int(7942) g = bool(0) #同 g = False 另 0,'',None 以外的變數會轉為 True h = float(True) #同 h = 1.0 i = str() #空字串 j = list() #空串列 k = dict() #空字典 #... ``` --- ## 輸出和輸入 ```python= print ('輸出內容') a = input ('輸入提示') #input() 在程式中被當作一個字串變數 print ('你輸入了:', a)) ``` --- ### 練習 1 step1: 在colab 使用 input() 函式 輸入,並 使用 type() 觀察輸入進去的資料型態 step2: 想辦法使你的輸入轉為 整數 型態 並印出來 >hint: input(),type() --- ## 運算元 Python 的運算遵循四則運算,會先乘除後加減 | 算數運算 | 加 | 減 | 乘 | 除 | | -------- | --- | --- | --- | --- | | 符號 | + | - | \* | / | | 整除 | 取餘 | 次方 | | ---- | ---- | ---- | | // | % | ** | --- | 邏輯運算 | 且 | 或 | 非 | 比較位置 | | -------- | --- | --- | --- | ---- | | 符號 | and | or | not | is | | 算數邏輯運算 | 大於 | 小於 | 等於 | | ------------ | ---- | ---- | ---- | | 符號 | > | < | == | | 不小於 | 不大於 | 不等於 | | ------ | ------ | ------ | | >= | <= | != | --- ### 練習 2.1 反向BMI 計算機 輸入 BMI與身高,而後印出體重 ```python= def reverse_BMI(BMI,H): return BMI*(H**2) ``` --- ## 條件運算 | 語法 | 說明 | | ------ | --- | | `if():` | ``()``中條件成立時執行 | | `elif():` | 上一個`if()`或`elif()`不成立時``()``中條件成立時執行,可無限接續 | | `else:` | 上一個`if()`或`elif()`不成立時執行 | --- ### 練習 2.2 閏年判斷,輸入1個整數讓程式判斷是否為閏年 閏年定義: 4的倍數是閏年,但100的倍數不是閏年,400的倍數是閏年 輸入:西元年分 輸出:是否為閏年 ```python= def leap_year(year): if (year%4 == 0 and year%100 != 0 ) or year%400 == 0: return True return False ``` --- ## 串列 `list` | 語法 | 說明 | | --------------------------- | ------------------ | |`x = [<元素1>,<元素2>,...]`| 定義串列 | | `x[<索引值>]` | 存取元素 | | `x[<索引值>:<索引值>]` | 取範圍串列 | --- | 函數 | 說明 | | ----------------------- | ------------------ | | `x.append(<元素>)` | 在串列最後新增元素 | | `x.insert(<索引>,<元素>)` | 插入元素 | | `x.remove(<元素>)` | 移除指定元素 | | `x.pop(<索引值(選填)>)` | 移除指定索引元素 | --- ## 字典 `dict` | 語法 | 說明 | | ----------------------------------- | ------------------ | | `d = {<索引>:<值>,<索引>:<值>,...}` | 定義字典 | | `d[<索引>] = 值` | 新增或覆蓋現有索引與值 | | `d.setdefault(<索引>,<值>)` | 不覆蓋新增索引與值 | --- ## for 迴圈 ### range 用法 ex: ```python= for i in range(10): print(i) ``` 印出 0~9 --- ### list 用法 ex: ```python= list=[1,2,3,4,5,6] for x in list: print(x) ``` 印出 list 中所有的元素 --- ### dict 用法 ex: ```python= dic={'a':10,'b':20,'c':30} for i in dic.keys(): print(i,':',dic[i]) ``` ```python= dict={'a':10,'b':20,'c':30} for k,v in dict.items(): print(k,':',v) ``` 印出 dict 中所有的索引跟值 --- ### 練習3 輸入 n 輸出 n個"防風林"並且中間由"外還有"隔開 --- ex: input 3 output: 防風林 外還有 防風林 外還有 防風林 --- ### 練習4 輸入 n 使用for 迴圈製作一個 包函所有小於n的偶數的一個串列 並輸出 >hint: 使用取餘數,if() 與 append --- ## function ex: ```python= def foo(): print('hello') foo() ``` --- ### 參數 與 回傳 ex: ```python= def add(a,b): c=a+b return c ``` a b 為參數 c 為回傳值 --- ### 練習5 把 練習4 製作為 function 輸入改為參數 輸出改為回傳 並且重複執行 --- # 綜合練習 ## leetcode 使用教學 https://leetcode.com/ >leetcode 網址 ![](https://i.imgur.com/wZ564am.jpg) ![](https://i.imgur.com/61qLi57.jpg) ![](https://i.imgur.com/9YBQgzk.jpg) ![](https://i.imgur.com/XLEJE2F.jpg) ![](https://i.imgur.com/B26WAsS.jpg) > 1 runcode 初步測試你的程式是不是對的 > 2 Sumit 繳交程式 ## two sum ```python= class solution: def twoSum(self,nums: List[int], target: int) -> List[int]: #規定了參數的型別與名稱,方便後續使用 for i in range(len(nums)): for j in range(i + 1, len(nums)): if nums[j] == target - nums[i]: return [i, j] ``` ## 再試試看這題題 https://leetcode.com/problems/sqrtx/ 先不要理這下面這行 > "Note: You are not allowed to use any built-in exponent function or operator, such as pow(x, 0.5) or x ** 0.5." ## valid-parentheses https://leetcode.com/problems/valid-parentheses/ 檢查括號是否成對 ## climbing-stairs https://leetcode.com/problems/climbing-stairs/ 樓梯有幾種走法,各位高中應該都教過(費氏數列) ## toeplitz-matrix https://leetcode.com/problems/toeplitz-matrix/ 檢查矩陣的數值 ## 其他題目 https://hackmd.io/xBIWdqktQPyAQBEUfyKAlw <style>hr{display:none;}</style>

    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