中正機研
      • 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 No publishing access yet

      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.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      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
    • Make a copy
    • 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 Note Insights Versions and GitHub Sync Sharing URL Help
Menu
Options
Engagement control Make a copy 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 No publishing access yet

    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.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    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: Lesson 2 # 簡報的名稱 tags: Python Tutorial # 簡報的標籤 slideOptions: # 簡報相關的設定 # theme: solarized # 顏色主題 transition: 'fade' # 換頁動畫 parallaxBackgroundImage: 'https://cdn.discordapp.com/attachments/887196342135451652/889752537513750588/image0.png' parallaxBackgroundSize: '2100px 1000px' defaultTiming: 120 --- <!-- 自訂CSS設定 --> <style> .hint { } .comment { font-size:70%; color: rgba( 200, 200, 200, 0.9 ); } .tag { padding: .4em; padding-bottom: .3em; margin: 0; font-size: 95%; font-family: Consolas; background-color: rgba( 200, 200, 200, .3 ); border-top-left-radius: 10px; border-bottom-left-radius: 10px; border-top-right-radius: 10px; border-bottom-right-radius: 10px; } .block { width:auto; padding: .05em; font-family: Consolas; border-top-left-radius: 10px; border-bottom-left-radius: 10px; border-top-right-radius: 10px; border-bottom-right-radius: 10px; background: rgba(255, 255, 255, .1); display:block; } </style> <!-- 內建CSS設定 --> <style> /* 單行codeblock */ .markdown-body code, .markdown-body tt{ padding: 0px; padding-top: .2em; padding-bottom: .2em; margin: 0; font-size: 85%; background-color: rgba( 0, 0, 0, .04 ); border-radius: 3px; } </style> # <span class="block"> Lesson 2 </span> --- ## 迴圈 ---- ### for 除了上次提到的while迴圈 for 迴圈在Python更常見 用它來**遍歷**一個**容器** ---- 甚麼是容器? 顧名思義 容器用來裝各種型態的資料 遍歷指將所有容器中的元素走過一遍 接下來式幾種Python中內建的容器 --- ### list 串列 ---- 用中括號`[]`括起來 ```python ['Python', 'Java', 'C++'] ``` 可以放不同型態的資料哦 ```python [8129,'FRC'] # OK ``` ---- 使用 [ ] 來取得**索引值**對應的資料 每個資料都有相應的索引值 跟點名表一樣 **在程式語言中索引值從0開始算起** ``` python l = ['Python', 'Java', 'C++'] print(l[0]) # 'Python' ``` ---- ### 示意圖: ![](https://i.imgur.com/Zcphyqm.png) ---- 如果索引值沒有對應的資料 也就是索引超過了串列的長度 ``` py l = ['Python', 'Java', 'C++'] print(l[3]) # IndexError: list index out of range ``` 會報錯 `IndexError` ---- 也可以在list裡放入另一個list(2維) ``` python l = [['Python', 'Ruby'], ['Java', 'C++']] print(l[0][0]) # 'Python' ``` ---- ### 示意圖: ![](https://i.imgur.com/QK1c8t9.png) --- ### slice 切片 用於取得一段的資料 ---- 先看一個範例 ``` python l = ['Python', 'Java', 'C++'] print(l[0:2]) # ['Python', 'Java'] ``` 中括號裡的0:2稱為**slice** ## <font color="red"> **slice不包含結束位置** </font> ---- 所以`0:2`只會取得 `l[0]`和`l[1]` ---- slice有三個值可以調整 {`start`|起始位置}:{`stop`|結束位置}:{`step`|間隔} ## <font color="red"> **slice不包含結束位置** </font> ---- ```python l = [0,1,2,3,4,5,6] print(l[0:5:2]) # 0 2 4 ``` --- ## str 字串 也就是字元形成的串列 ``` python s = 'Python' print(s[2]) # 't' ``` --- ## tuple 元組 不可變的list ``` python t = ('Python', 'Java', 'C++') print(t[0]) # 'Python' ``` ---- 當你忘記... ``` python t = ('Python', 'Java', 'C++') t[0] = 'Ruby' # TypeError: 'tuple' object does not support item assignment ``` ---- 示意圖: ![](https://i.imgur.com/b00WJeD.png) --- ### dict 字典 字典是由鍵(key)與值(value)構成 `{key: value}` 鍵是個不可變物件(ex.字串 元組) ``` python d = {'FRC' : 8129, 'Python': 'Object'} print(d['FRC']) # 8129 ``` ---- 示意圖: ![](https://i.imgur.com/raGeJZb.png) ---- dict 其實就是 mapping 的意思(對應) dict的鍵值也可以被遍歷 之後會再說明 在C++中, <map.h>有和dict一樣的功能 --- ### set 集合 和list很類似, 但集合內不會包含重複的資料 ---- 可以這樣用: ``` python l = [1, 1, 3, 4, 7, 7] l = set(l) print(l) # {1, 3, 4, 7} ``` ---- 集合是沒有順序的 ```python s = {8,1,2,9} print(s[0]) # TypeError: 'set' object is not subscriptable ``` --- 了解甚麼是容器後 實作看看for迴圈 ```python= # 首先創建一個陣列 languages = ['Python', 'Java', 'C++'] ``` ```python=3 # 用for迴圈遍歷它 for lang in languages: print(lang) # 'Python' # 'Java' # 'C++' ``` --- ### range ---- range(start, stop[, step]) ---- 先來看個例子 ```python= for i in range(5): print(i) ``` 以上的程式會依序輸出0~4 (以0為起始) ---- 再來個例子 ```python start = 1 end = 3 for i in range(start, end): print(i) # 1 # 2 ``` 以1為起始 2結束 **不包括最後一個數字** ---- 也可以指定他的間隔 ```python start = 1 end = 6 separation = 2 for i in range(start, end, separation): print(i) # 1 # 3 # 5 ``` 以1為起始, 每次加2, 當大於等於6時就跳出迴圈 ---- 舊版本的range會生成一個對應大小的串列 現在經過優化之後則會生成一個range物件 ```python= print(range(5)) # range(0, 5) ``` 如果想把它變成一個串列要使用list() ```python=4 print(list(range(5))) # [0,1,2,3,4] ``` ---- <style> @keyframes rainbow { from { color: red; letter-spacing: 0; } to{ color: blue; letter-spacing: 0.5em; } } </style> <b style="display: block; text-align: center; animation: 3s ease alternate rainbow infinite;"><h1>謝謝聆聽</h1></b>

    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
    Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    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