Python 3 非程式設計師教程/使用模組
這是本章的打字練習(將其命名為 cal.py(import 實際上會尋找一個名為 calendar.py 的檔案並讀取它。如果檔名為 calendar.py 並且它看到一個 "import calendar",它會嘗試讀取它本身,這在最好的情況下也很差。))
import calendar
year = int(input("Type in the year number: "))
calendar.prcal(year)
這是我得到的部分輸出
Type in the year number: 2001
2001
January February March
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 6 7 1 2 3 4 1 2 3 4
8 9 10 11 12 13 14 5 6 7 8 9 10 11 5 6 7 8 9 10 11
15 16 17 18 19 20 21 12 13 14 15 16 17 18 12 13 14 15 16 17 18
22 23 24 25 26 27 28 19 20 21 22 23 24 25 19 20 21 22 23 24 25
29 30 31 26 27 28 26 27 28 29 30 31
(我跳過了一些輸出,但我認為你明白了。那麼這個程式做了什麼?第一行 import calendar 使用了一個新的命令 import。命令 import 載入一個模組(在本例中是 calendar 模組)。要檢視標準模組中可用的命令,請檢視 python 的庫參考(如果您下載了它),或訪問 https://docs.python.club.tw/3/library/。如果您檢視日曆模組的文件,它列出了一個名為 prcal 的函式,它列印一年中的日曆。calendar.prcal(year) 使用此函式。總之,要使用一個模組,請 import 它,然後使用 module_name.function 來表示模組中的函式。另一種編寫程式的方式是
from calendar import prcal
year = int(input("Type in the year number: "))
prcal(year)
此版本從模組中匯入特定函式。以下是用 Python 庫的另一個程式(將其命名為 clock.py 之類)(同時按下 Ctrl 和“c”鍵來終止程式)
from time import time, ctime
prev_time = ""
while True:
the_time = ctime(time())
if prev_time != the_time:
print("The time is:", ctime(time()))
prev_time = the_time
部分輸出如下
The time is: Sun Aug 20 13:40:04 2000
The time is: Sun Aug 20 13:40:05 2000
The time is: Sun Aug 20 13:40:06 2000
The time is: Sun Aug 20 13:40:07 2000
Traceback (innermost last):
File "clock.py", line 5, in ?
the_time = ctime(time())
KeyboardInterrupt
當然,輸出是無限的,所以我取消了它(或者至少輸出繼續,直到按下 Ctrl+C)。該程式只是執行一個無限迴圈(True 始終為真,因此 while True: 永久執行)並且每次檢查時間是否已更改,如果已更改,則列印它。請注意,在 from time import time, ctime 行中如何使用 import 語句後的多個名稱。
Python 庫包含許多有用的函式。這些函式為您的程式提供了更多功能,其中許多函式可以簡化 Python 中的程式設計。
將 決策 部分中的 high_low.py 程式改寫為使用 0 到 99 之間的隨機整數,而不是硬編碼的 7。使用 Python 文件找到合適的模組和函式來執行此操作。
將 決策 部分中的 high_low.py 程式改寫為使用 0 到 99 之間的隨機整數,而不是硬編碼的 7。使用 Python 文件找到合適的模組和函式來執行此操作。
from random import randint
number = randint(0, 99)
guess = -1
while guess != number:
guess = int(input ("Guess a number: "))
if guess > number:
print("Too high")
elif guess < number:
print("Too low")
print("Just right")
有時您想使用 Python 安裝中未提供的 Python 模組。您也可以匯入它們,但必須在您的計算機上安裝它們。
當 python 讀取 import 命令時,它首先檢查您目錄中的檔案,然後檢查 site-packages 或預安裝的模組。要建立您自己的模組,只需在當前目錄中建立一個 .py 檔案並使用以下命令
import module
這將嘗試從您的當前目錄匯入檔案 module.py,如果找不到,則從 site-packages 和預打包的模組中匯入。將 module 更改為您建立的 .py 檔案的名稱將匯入該檔案。
但是,當它匯入模組時,它基本上會將該檔案作為程式啟動,因此那裡的任何程式碼都將被執行。您希望將所有程式碼分組到函式中。
在 python 中,變數 __name__ 將為您提供當前程式的名稱。如果匯入的模組列印 __name__ 變數,則它將列印模組的名稱。如果當前檔案列印 __name__ 變數,它將列印 __main__,以表明它是主程式。
如果 if 語句檢查 name 變數並在程式為主程式時執行程式碼,它可以繞過匯入模組時建立的意外執行問題。例如,假設您有一個檔案,它執行一些程式碼。它還有一個您希望在另一個程式中使用的函式。但是,您只想要該函式,而不是執行程式碼。透過設定下面的程式碼,它只會在點選或啟動該檔案時執行程式碼,而不會在匯入該檔案時執行程式碼。
if __name__ == '__main__':
pass
在這種情況下,如果該檔案被執行但沒有被匯入,它將執行 pass 命令。您可以用您希望在沒有匯入的情況下執行的程式碼替換 pass 命令。只需記住縮排程式碼。
pip 模組是一個隨 python 安裝一起提供的模組,充當模組下載器/管理器。您可以使用 pip 從網際網路上下載其他模組。
pip 模組不用於 python 直譯器,而是透過命令列執行。要使用它,請開啟您的命令列直譯器(對於 Windows,它是命令提示符,對於 Mac/Linux,它是終端)並輸入以下程式碼
py3 -m pip install module
或備用程式碼
pip install module
這將嘗試從使用者提交的 python 模組資料庫中下載和安裝模組。Module 可以更改為模組的名稱。