中正機研
      • 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
      • Invitee
    • 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
    • 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 Sharing URL Help
Menu
Options
Versions and GitHub Sync 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
Invitee
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: Lesson 3 # 簡報的名稱 tags: Python Tutorial # 簡報的標籤 slideOptions: # 簡報相關的設定 # theme: solarized # 顏色主題 transition: 'fade' # 換頁動畫 parallaxBackgroundImage: 'https://cdn.discordapp.com/attachments/887196342135451652/889752537513750588/image0.png' parallaxBackgroundSize: '2100px 1000px' defaultTiming: 120 --- {%hackmd @ZZRT/CSS %} # 函式 包裝程式裡相同的部分 ---- `def`關鍵字 > def func(para) **def** {my_function|函式名稱}({para1, para2|參數}): ---- 以縮排定義函式區塊 ```python def func(): # 函式區塊 ``` ---- 如果想讓參數帶有預設值呢? ```def function(a=0)``` ---- 函式裡的==區域變數==不能被外面使用 ```python def func(): var = 0 print(var) # Error ``` ---- 可以調用外面的函數 但**不能改變** ```python= var = 0 def func(): print(var) # OK var += 1 #Error ``` ```python=4 global var ``` ---- return 回傳值 ```python def func(): return ``` Python的函式可以當作參數傳遞給變數或其他函式 而這種函式也稱為==一級函式== ---- ```python= def func(f): print('call function') return f() def sayHi(): print('hi') func(sayHi) # callfunction # hi ``` ---- <ol class=quiz> Q1. 下列何者是python定義函式的關鍵字 <li data-ans="x">func</li> <li data-ans="x">function</li> <li data-ans="o">def</li> <li data-ans="x">define</li> </ol> ---- Q2. 撰寫一個sum函式 用於相加兩數 :::spoiler 提示1: def sum` `a` ` b` ` ` `   `    ` a+b ::: :::spoiler 提示2: `)` `(` `return` `,` `:` ::: ---- ```python a = 0 def func(): a = 1 func() print(a) ``` <ol class=quiz> Q3. 上述程式會輸出甚麼? <li data-ans="o">0</li> <li data-ans="x">1</li> <li data-ans="x">Error</li> </ol> ---- ```python a = 1 def func(): a -= 1 func() print(a) ``` <ol class=quiz> Q3. 上述程式會輸出甚麼? <li data-ans="x">0</li> <li data-ans="x">1</li> <li data-ans="o">Error</li> </ol> ---- ```python a = 0 def func(): global a a = 1 def func2(): a = 2 func2() func() print(a) ``` <ol class=quiz> Q5. 上述程式會輸出甚麼? <li data-ans="x">0</li> <li data-ans="o">1</li> <li data-ans="x">2</li> </ol> --- ### Lambda 匿名函式 ---- - 簡潔 - 不需要函式名稱 - 適用於小型運算 - 只有1行 - Java, C# 也支援喔~ ---- 使用方法: **lambda** `parameters`**:** `expression` - lambda: key word - parameter: 參數 - expression: 運算式 ---- 使用範例: ``` python power = lambda x, y: x**y; print(power(25, 2)) # 625 ``` ---- 也可以用filter來跑list ``` python a = [1, 2, 3, 4, 5] result = filter(lambda x: x>3, a) print(list(result)) # [4, 5] ``` --- ### 註解 \# 單行註解 """ """ 放在函式的第一行 功能跟多行註解相似 正確名稱是 **多行字串** 它也是字串 可以被`print` 也可以像普通字串一樣操作 特點: 文字都會原封不動的顯示 ---- <ol class=quiz> Q2. 如何註解一段文字? <li data-ans="x">// 註解</li> <li data-ans="o"># 註解</li> <li data-ans="x">/* 註解 */</li> <li data-ans="x">``` 註解 ```</li> <li data-ans="o">''' 註解 '''</li> </ol> --- # 練習題題目咯~~ ---- 給定經過多次對折旋轉的紙的邊長 以對折次數 回推原本的紙張 第一行依序為紙的長(縱)、寬(橫) 接下來有數個動作 1: 上下對折 2: 左右對折 3: 旋轉 直到輸入0代表操作結束 輸出: 以*拼成未經對折的紙張 (後面有測資, 共2頁) ---- ``` 輸入: 1 3 1 1 2 0 輸出: ****** ****** ****** ****** ``` ---- ``` 輸入: 2 3 1 3 1 0 輸出: **** **** **** **** **** **** ``` ---- :::spoiler (點我show答案) ``` python def draw(h,w): print(('*'*w+'\n')*h) height, width = map(int,input().split()) process = [] while True: p = input() if p == '0': break process.append(p) for p in process[::-1]: # 以相反順序回推 if p == '1': height *= 2 elif p == '2': width *= 2 elif p=='3': height, width = width, height draw(height,width) ``` :::

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