資訊之芽 Python 語法班 2023/04/16
鄧人豪
具體來說就是:
它的概念與用處就是:
# 1. import <模組名> import math # 2. import <模組名> as <自訂名稱> import math as m # 3. from <模組名> import <該模組內的東西> # 模組內的東西包括:<子模組>, <函式>, <變數>, <Class> from math import factorial, sqrt, pi # 4. from <模組名> import <該模組內的東西> as <自訂名稱> from math import factorial as fact
import math print(math.factorial(4)) # 1 * 2 * 3 * 4 = 24
import math as m print(m.factorial(4)) # 1 * 2 * 3 * 4 = 24
# 模組內的東西包括:<子模組>, <函式>, <變數>, <Class> from math import factorial, sqrt, pi print(factorial(4)) # 1 * 2 * 3 * 4 = 24 print(sqrt(2)) # 1.4142135623730951 print(pi) # 3.141592653589793
from math import factorial as fact print(fact(4)) # 1 * 2 * 3 * 4 = 24
# from <模組名> import * from math import * print(factorial(4)) # 1 * 2 * 3 * 4 = 24 print(sqrt(2)) # 1.4142135623730951 print(pi) # 3.141592653589793 # Symbol table print(dir()) # 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e'...
import numpy as np Traceback (most recent call last): File "python_path", line 1, in <module> import numpy as np ModuleNotFoundError: No module named 'numpy'
解決辦法
pip install numpy
!pip install numpy
import math # floor(a): a 的下高斯, ceil(a): a 的上高斯 print(math.floor(1.9), math.ceil(2.1)) # 1 3 # pow(a, b): a 的 b 次方, sqrt(a): a 的開根號 print(math.pow(2, 10), math.sqrt(2)) # 1024.0 1.4142135623730951 # gcd(a, b): a, b 的最大公因數 print(math.gcd(18, 45)) # 9 # 常數 pi 與 e print(math.pi, math.e) # 3.141592653589793 2.718281828459045 # sin(a), cos(a) (單位為弧度 radian) print(math.sin(math.pi/2), math.cos(math.pi/4)) # 1.0 0.7071067811865476
import string # 小寫字母:abcdefghijklmnopqrstuvwxyz print(string.ascii_lowercase) # 大寫字母:ABCDEFGHIJKLMNOPQRSTUVWXYZ print(string.ascii_uppercase) # 十六進制位元:0123456789abcdefABCDEF print(string.hexdigits) # 標點符號:!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ print(string.punctuation) # 數字:0123456789 print(string.digits)
import random # [0, 1) 之間的浮點數 print(random.random()) # uniform(a, b): [a, b] 之間的浮點數 print(random.uniform(1, 10)) # randint(a, b): [a, b] 之間的整數 print(random.randint(1, 10))
numbers = [i for i in range(10)] print(numbers) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # choice(a): 從 list a 中隨機挑選一個 print(random.choice(numbers)) # shuffle(a): 直接將 list a 打亂 random.shuffle(numbers) print(numbers) # 被打亂後的 numbers
import os # 取得當前目錄 print(os.getcwd()) # 列出目錄底下的檔案,以根目錄為例 print(os.listdir('/')) # 組合成完整路徑名 print(os.path.join('/usr', 'bin')) #/usr/bin # 檢查路徑或檔案是否存在 print(os.path.exists(path)) # 刪除檔案 print(os.remove('trash_file'))
import sys # 命令列的參數 (list) print(sys.argv) # Python 的版本 print(sys.version) # 操作系統平台名稱 print(sys.platform) # 標準輸出 print(sys.stdout)
import time # 從 1970/1/1, 00:00:00 到現在經過的秒數 print(time.time()) # 目前時間 print(time.localtime()) # 格式化時間 print(time.asctime()) # 暫停 3 秒 time.sleep(3) # 時間會比上面的多 3 秒 print(time.time())
import datetime # 現在日期與時間 t = datetime.datetime.now() # 2023-04-05 03:09:36.040984 print(t) # 取得特定時間 print(t.year, t.month, t.day) # 2023 4 5 # 將時間轉換成指定的字串格式 print(t.strftime("%Y %m %d %A")) # 2023 04 05 Wednesday
.py
的 python 檔案number_list.py
的 python 檔案,假如你主要使用的檔案是 main.py
,那目錄大概會長這樣: python_module/ 頂層目錄
├── main.py(main.ipynb) ├── 主要程式
├── number_list.py ├── 模組檔案
number_list.py
中定義想要的函式與變數def get_number_list(num): # 取得範圍為 [0, num) 的整數 list number_list = [i for i in range(num)] return number_list
main.py
或 main.ipynb
中 import 該 moduleimport number_list num_list = number_list.get_number_list(10) print(num_list) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
注意:假如是在 .ipnyb
檔案中 import 其他檔案,建議加上以下程式碼。
避免在更改 module 內容後,.ipynb
檔案的 kernel 中記得的仍然是舊的 module 內容
%load_ext autoreload %autoreload 2
json 轉 python
import json # json 格式的字串 json_string = '{ "name": "thomas", "age": 20, "email": ["email1@example.com", "email2@example.com"]}' # 將 json 格式的字串轉為 python (這個範例中是被轉為 python dictionary) py_dict = json.loads(json_string) print(type(py_dict)) # <class 'dict'> print(py_dict['name'], py_dict['age'], py_dict['email']) # thomas 20 ["email1@example.com", "email2@example.com"]
python 轉 json
import json # 一個 python object (dictionary) py_dict = { "name": "thomas", "age": 20, "email": ["email1@example.com", "email2@example.com"] } # 將 python object 轉為 json 格式 json_string = json.dumps(py_dict) print(type(json_string)) # <class 'str'> print(json_string) # {"name": "thomas", "age": 20, "email": ["email1@example.com", "email2@example.com"]}
將 list 資料寫入 csv 檔案
import csv # 開啟要寫入的 csv 檔案 with open('output.csv', 'w', newline='') as csvfile: # 建立 csv 檔寫入器 writer = csv.writer(csvfile) # 寫入一列 header writer.writerow(['name', 'height', 'weight']) # 寫入幾列儲存資料 (list) writer.writerow(['John', 175, 60]) writer.writerow(['Thomas', 180, 57]) writer.writerow(['Marry', 160, 49])
將 dictionary 資料寫入 csv 檔案
import csv with open('output.csv', 'w', newline='') as csvfile: # 定義欄位名稱 (header) header = ['name', 'height', 'weight'] # 將 dictionary 寫入 csv 檔 writer = csv.DictWriter(csvfile, fieldnames=header) # 寫入第一列的欄位名稱 writer.writeheader() # 寫入幾列儲存資料 (dictionary) writer.writerow({'name': 'John', 'height': '175', 'weight': '60'}) writer.writerow({'name': 'Thomas', 'height': '180', 'weight': '57'}) writer.writerow({'name': 'Marry', 'height': '160', 'weight': '49'})
你的 output.csv
內容應該會長這樣:
name,height,weight John,175,60 Thomas,180,57 Marry,160,49
讀取 csv 檔案到 list 中
import csv # 開啟 csv 檔案 with open('output.csv', 'r', newline='') as csvfile: # 讀取 csv 檔案內容 rows = csv.reader(csvfile) # 以迴圈輸出每一列 (包括 header) for row in rows: print(row) # 每一行都是一個 list
讀取 csv 檔案到 dictionary 中
import csv # 開啟 csv 檔案 with open('output.csv', 'r', newline='') as csvfile: # 讀取 csv 檔內容,將每一列轉成一個 dictionary rows = csv.DictReader(csvfile) # 以迴圈輸出每一列 (不包括 header) for row in rows: print(row) # 每一行都是一個 dictionary
假如我不想用 comma(逗號)分隔資料,
而是用 colon(冒號)分隔呢?
方法:在寫入時,使用
csv.writer(..., delimiter=":")
import csv # 開啟要寫入的 csv 檔案 with open('output_colon_separated.csv', 'w', newline='') as csvfile: # 建立 csv 檔寫入器 writer = csv.writer(csvfile, delimiter=":") # 寫入一列 header writer.writerow(['name', 'height', 'weight']) # 寫入幾列儲存資料 (list) writer.writerow(['John', 175, 60]) writer.writerow(['Thomas', 180, 57]) writer.writerow(['Marry', 160, 49])
你的 output_colon_separated.csv
內容應該會長這樣:
name:height:weight John:175:60 Thomas:180:57 Marry:160:49
讀取 output_colon_separated.csv
時要用
csv.reader(..., delimiter=":")
import csv # 開啟 csv 檔案 with open('output_colon_separated.csv', 'r', newline='') as csvfile: # 讀取 csv 檔案內容 rows = csv.reader(csvfile, delimiter=":") # 以迴圈輸出每一列 (包括 header) for row in rows: print(row) # 每一行都是一個 list