or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
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.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
Python Module
Sean 韋詠祥
Note:
日期:2023-07-21(五)Day 2
課程時間:20:05 - 21:30
18:30 Function
20:00 Module
21:30 End
20:05 開場
20:10 練習 1:三角函數
20:15 自己寫 Module
20:20 練習 2:實作階乘函式
20:30 __all__
20:35 常見 Module(一)
20:40 練習 3:亂數產生器
20:50 常見 Module(二)
20:55 練習 4:RegEx
21:05 專案開發
21:10 常見雷點
21:20 Coding Style
21:30 彩蛋
21:30 End
這堂課你會學到…
Note:
前一堂:上午 Hugo 教基礎容器、jayinnn 講 Function
後一堂:Day 3 Vincent 講 Packages
簡報連結記得放 Camp 網站
為何需要 Module
Note:
PyDoc 的 6. Modules 開頭就講了
https://docs.python.org/3/tutorial/modules.html
什麼是 Module
如何使用 Module
引入單個模組
Note:
引入模組內的某些功能
Note:
引入模組內的全部功能
Note:
幫模組取別名
練習 1:三角函數
用 Python 計算以下數值
\[ 3^5 \text{ = ?} \\ \sqrt{2} \text{ = ?} \\ \cos (\frac{\pi}{3}) \text{ = ?} \]
Hint: 全部在
math
裡面sqrt
,cos
,pow
,pi
自己寫 Module
建立一個名為
fibo.py
的模組檔案在 VS Code 中,請按 ^N (Ctrl-N) 新增檔案
寫完後用 ^S 存檔
Note:
來源:PyDoc Modules Tutorial
https://docs.python.org/3/tutorial/modules.html
使用方式
或是這樣
在 VS Code 中,點右上角 ▷ 執行程式
練習 2:實作階乘函式
在
sitcon.py
中,自己定義fact()
函式Hint: 可以在
def fact()
中遞迴呼叫fact()
函式模組中的 __all__ 特殊變數
當有一些函式不希望被
import *
讀進來在
from sitcon import *
的時候只有
__all__
裡面寫到的fact
會被 import 進來Note. 但可以被
import sitcon
或from sitcon import xxx
取用模組中的 __all__ 特殊變數
其他特殊變數
在模組中,還有可以加上…
常見的 Module
Note:
The Python Package Index (PyPI) is a repository of software for the Python programming language.
https://pypi.org/
請大家跟著做
可以用 Tab 或 __all__ 找到有效函式、常數
Math 數學
Note:
搜尋 "py math"
Ref: https://docs.python.org/3/library/math.html
String 字串
Note:
Ref: https://docs.python.org/3/library/string.html
ASCII Table
Ref: https://www.asciitable.com/asciifull.gif
sys 系統參數
Note:
Ref: https://docs.python.org/3/library/sys.html
Random 隨機
Note:
Ref: https://docs.python.org/3/library/random.html
只要取得幾百個隨機數,就能精準預測後面的其他隨機輸出
https://github.com/tna0y/Python-random-module-cracker
Random 隨機
練習 3:亂數產生器
輸入格式
第 1 行輸入數字 N 代表長度(N ≥ 5)
第 2 行輸入 0 或 1,代表是否要包含大小寫字母
第 3 行輸入 0 或 1,代表是否要包含數字和標點符號
保證第 2 3 行不會同時為 0
輸出要求
使用前面提到的
strings
及random
函式庫每次產生 5 個長度為 N 且符合要求的隨機字串
(就算輸入要求有數字,輸出也可以沒出現數字,只要正常隨機就好)
常見 Module(二)
OS 作業系統介面
Note:
Ref: https://docs.python.org/3/library/os.html
Time 時間
Note:
Ref: https://docs.python.org/3/library/time.html
Datetime 日期與時間
Note:
Ref: https://docs.python.org/3/library/datetime.html
datetime 底下有 date, time, datetime, timedelta, timezone 等
晚點有空再提 __repr__()
慣用的套件縮寫
re 正規表達式
Note:
https://docs.python.org/3/library/re.html
練習 4:正規表達式
在以下
???
部分填入正確函式並撰寫 RegEx 語句,分別從三個
txt
提取答案Python 專案開發
拆成幾十個模組怎麼辦?
總不能全部丟在同個目錄吧
目錄架構
一個專案可能長得像這樣
何謂 __init__.py
我們可以 import 一個 .py 檔案,那資料夾呢?
因此只有包含
__init__.py
的資料夾可以被引入此檔案中會放初始化模組用的程式碼
並且用
__all__
特殊變數,指定同目錄下要引入哪些檔案或子資料夾Note:
如果 import 一個套件,就把裡面所有子資料夾全部讀進來,那可能會遇到一些撞名的資料夾或檔案,導致不必要的麻煩
模組之間互相引用
常見雷點
NameError
找不到這個變數
解決方法
確定有 import 相關模組
ModuleNotFoundError
找不到這個模組
解決方法
使用 pip 安裝相應套件
pip not found
解決方法
到 Python 官網安裝
Note:
在這邊確保大家的環境是一致的
AttributeError 檔案撞名
先上程式碼
錯誤訊息
為什麼呢?
Note:
這裡的 import math 是自己 math.py 的意思
ImportError 遞歸引用
Note:
即使此案例中 function 本身沒有造成迴圈也會出錯
補充:Coding Style
每個 import 分開寫
寫 import 的位置
排序方式
排序方式
特殊變數擺放位置
__future__
等特殊 import關於 import * 用法
補充用法:__repr__()
Note:
參考文章:repr 與 str 雜談
https://ithelp.ithome.com.tw/articles/10194593
跟 import 有關的 Python 小彩蛋
跟 import 有關的 Python 小彩蛋
參考資料 / 延伸閱讀
Thanks
投影片連結:https://hackmd.io/@Sean64/py-module-sitcon2023
這份投影片以 創用 CC - 姓名標示 授權公眾使用,原始碼及講稿請見 此連結。