iT邦幫忙

2025 iThome 鐵人賽

DAY 18
0
Software Development

學會 Python 不可怕:我每天學一點的 30 天筆記系列 第 18

Day18 : 模組與套件 – import、常用模組

  • 分享至 

  • xImage
  •  

Python 內其實已經幫我們準備了大量的模組(module)和套件(package),就像工具箱,可以直接拿來套用

1. 什麼是模組(module)

  • 模組:一個 .py 檔案,裡面有程式碼(函式、變數、類別等)
  • 可以使用 import 把模組引入程式,直接使用裡面的功能
    https://ithelp.ithome.com.tw/upload/images/20251002/20178872ALsPXNVIW6.png
    結果會輸出
    https://ithelp.ithome.com.tw/upload/images/20251002/20178872TC5tYA1Cqx.png
  • Python 內本身就有一個「數學工具箱」,叫 math。import math 就是把這個工具箱搬進來,這樣就可以直接用裡面的功能
  • sqrt 是 square root(平方根)的縮寫,結果會有小數點 .0,因為 sqrt() 的回傳值是「浮點數」,就算結果剛好是整數,Python 也會加 .0
  • pi 是數學裡的 π(圓周率)

2. import 的幾種用法
(1) 引入整個模組
https://ithelp.ithome.com.tw/upload/images/20251002/20178872QC0NZvQ2Bn.png
每輪都會隨機輸出數字
https://ithelp.ithome.com.tw/upload/images/20251002/20178872LNucXS346i.png

  • random 這個模組就像一個「抽籤工具箱」,它可以產生隨機數字,就像抽籤、擲骰子一樣,沒有人能事先知道結果
  • randint(a, b) 的意思是:隨機挑一個整數,範圍是從 a 到 b(包含 a 和 b)
  • 這裡的 random.randint(1, 10) → 會隨機給一個 1 到 10 的整數

(2) 引入模組中的特定功能
https://ithelp.ithome.com.tw/upload/images/20251002/2017887250KtnSRYuZ.png
結果會輸出
https://ithelp.ithome.com.tw/upload/images/20251002/20178872wAdnVQ3877.png

  • from math import sqrt, pi 的意思是:我只想要用 math 裡面的 sqrt(開根號函式)和 pi(圓周率常數)
  • 這樣在後面寫程式的時候,就不用再寫 math.sqrt() 或 math.pi,可以直接用 sqrt()、pi

(3) 幫模組取別名
https://ithelp.ithome.com.tw/upload/images/20251002/20178872C7iQcEvaax.png
結果會輸出當下的時間
https://ithelp.ithome.com.tw/upload/images/20251002/20178872d0eQ7IeW95.png

  • datetime 是 Python 內建的「日期時間模組」
  • 這裡寫 as dt → 幫它取一個簡短的「暱稱」,之後就可以用 dt 代替 datetime
  • dt.datetime → 指的是 datetime 模組裡的 datetime 類別,它可以處理日期與時間
  • .now() → 代表「現在」,會回傳當下的日期和時間
  • 輸出格式會是 : 年-月-日 時:分:秒.微秒

3. 常用的內建模組
(1) random:隨機數
https://ithelp.ithome.com.tw/upload/images/20251002/20178872jHlBlqm0tl.png
每次輸出結果都是隨機的值
https://ithelp.ithome.com.tw/upload/images/20251002/20178872JixQrnrlP6.png

(2) math:數學運算
https://ithelp.ithome.com.tw/upload/images/20251002/20178872jqkxmQz1f6.png
結果會輸出
https://ithelp.ithome.com.tw/upload/images/20251002/20178872Jqh21q9KZS.png

  • factorial(n) → 計算 n 的階乘。階乘:n! = n × (n-1) × (n-2) ... × 1
    例如:5! = 5 × 4 × 3 × 2 × 1 = 120
  • ceil(x) → 無條件進位(往上取整數)。
    例如:3.2 → 就會往上變成 4
  • floor(x) → 無條件捨去(往下取整數)。
    例如:3.8 → 就會往下變成 3

(3) datetime:日期時間
https://ithelp.ithome.com.tw/upload/images/20251002/201788721rVmPXWaPG.png
結果會輸出
https://ithelp.ithome.com.tw/upload/images/20251002/20178872NVuaC9xFi0.png

  • strftime 就是「把時間變漂亮字串」的工具
  • "%Y-%m-%d" 格式規則:
    %Y → 四位數的年 (2025)
    %m → 月份 (10)
    %d → 日期 (02)
    → 最後只會顯示「2025-10-02」

4. 自訂模組
我們也可以自己寫一個 .py 檔當模組,然後在別的程式引入
先假設有一個 my_math.py:
定義一個 add 函式,在目前的程式檔裡面,並為這個新檔案命名為my_math.py
https://ithelp.ithome.com.tw/upload/images/20251002/20178872E9h3kDjwCv.png
import my_math 代表你想要「載入一個叫 my_math.py 的外部檔案」,所以前面才需要先建置一個my_math.py檔,這個時候 Python 會找到 my_math.py,並呼叫裡面的 add
https://ithelp.ithome.com.tw/upload/images/20251002/20178872PfL5oYEWGO.png
會輸出兩數相加結果68
https://ithelp.ithome.com.tw/upload/images/20251002/20178872R231ptznAb.png

5. 套件(package)

  • 套件 = 一個資料夾,裡面有很多模組。
  • 資料夾裡面通常有一個 init.py,讓 Python 知道這是套件
    numpy、pandas、matplotlib 都是很有名的 第三方套件(要用 pip install 安裝)
    https://ithelp.ithome.com.tw/upload/images/20251002/20178872VyRQpkityi.png

6. 練習1
https://ithelp.ithome.com.tw/upload/images/20251002/201788729eNWfjgjaB.png
結果會直到猜中數字為止才會跳出迴圈
https://ithelp.ithome.com.tw/upload/images/20251002/20178872L0kMStreQH.png

7. 練習2
https://ithelp.ithome.com.tw/upload/images/20251002/20178872vKhOGwAVCC.png
結果會輸出今天是星期幾
https://ithelp.ithome.com.tw/upload/images/20251002/20178872vbt0o8DDFM.png

  • datetime.date.today() : 取得今天的日期(不含時間),存到 today
  • today.weekday() : 取得今天是星期幾
  • 建立 weekdays 列表,把數字 0~6 對應到中文星期,用 weekdays[weekday_num] 取得今天的中文星期

8. 練習3
https://ithelp.ithome.com.tw/upload/images/20251002/20178872T2wFgSc2aX.png

  • 首先建立一個新檔案叫 greet.py,在檔案中定義一個函式 hello,接收一個參數 name
  • print(f"哈囉,{name}!") → 用 f-string 把名字代入訊息

https://ithelp.ithome.com.tw/upload/images/20251002/20178872vDbD7FFfKp.png

  • 再建立另一個 Python 檔案,並呼叫 greet.py
  • greet.hello("Jack") → 用模組名稱.函式名稱()呼叫函式

結果就會輸出
https://ithelp.ithome.com.tw/upload/images/20251002/201788725xTmVhVkPV.png


上一篇
Day17 : 函式進階 – 預設參數、可變參數、作用域
系列文
學會 Python 不可怕:我每天學一點的 30 天筆記18
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言