python常用模塊(math itertools functools sys shutil)使用講解
python常用模塊
1.math
math
模塊提供了一些數(shù)學函數(shù)和常數(shù),如圓周率、三角函數(shù)、對數(shù)函數(shù)等。
- 計算平方根:
import math sqrt_result = math.sqrt(9) print(sqrt_result) # 輸出:3.0
- 計算正弦和余弦:
import math angle = math.radians(30) # 將角度轉(zhuǎn)換為弧度 sin_result = math.sin(angle) cos_result = math.cos(angle) print(sin_result) # 輸出:0.5 print(cos_result) # 輸出:0.8660254037844387
- 計算指數(shù)和對數(shù):
import math exp_result = math.exp(1) # 計算 e 的 1 次方 log_result = math.log(exp_result) # 計算 e 為底數(shù)的對數(shù) print(exp_result) # 輸出:2.718281828459045 print(log_result) # 輸出:1.0
2. itertools
itertools
模塊提供了一些用于操作迭代對象的函數(shù),如無限迭代器、組合和排列等。
- 生成一個無限迭代器:
import itertools count = itertools.count(start=1, step=2) for i in range(5): print(next(count)) # 輸出:1, 3, 5, 7, 9
- 生成一個循環(huán)迭代器:
import itertools cycle = itertools.cycle("ABCD") for i in range(8): print(next(cycle)) # 輸出:A, B, C, D, A, B, C, D
- 生成排列和組合:
import itertools # 排列 for p in itertools.permutations("ABCD", 2): print("".join(p)) # 輸出:所有 2 個元素的排列 # 組合 for c in itertools.combinations("ABCD", 2): print("".join(c)) # 輸出:所有 2 個元素的組合
3. functools
functools
模塊提供了一些用于操作函數(shù)和可調(diào)用對象的工具。
- 使用
lru_cache
緩存函數(shù)結(jié)果:
import functools @functools.lru_cache(maxsize=None) # 使用 LRU 緩存策略緩存函數(shù)結(jié)果 def fib(n): if n < 2: return n return fib(n-1) + fib(n-2) print(fib(100)) # 輸出:354224848179261915075
- 使用
partial
固定部分參數(shù):
import functools def power(base, exponent): return base ** exponent square = functools.partial(power, exponent=2) cube = functools.partial(power, exponent=3) print(square(5)) # 輸出:25 print(cube(5)) # 輸出:125
4. sys
sys
模塊提供了一些與 Python 解釋器和運行環(huán)境交互的函數(shù)和變量。
- 獲取命令行參數(shù):
import sys print("Script name:", sys.argv[0]) # 輸出:腳本名(通常是文件名) print("Arguments:", sys.argv[1:]) # 輸出:命令行參數(shù)列表
- 獲取 Python 版本信息:
import sys print("Python version:", sys.version) # 輸出:Python 版本信息
- 獲取系統(tǒng)平臺信息:
import sys print("Platform:", sys.platform) # 輸出:系統(tǒng)平臺信息(如 'linux', 'win32' 等)
5. shutil
shutil
模塊提供了一些高級文件操作函數(shù),如文件復制、移動、刪除等。
- 復制文件:
import shutil shutil.copy("src.txt", "dst.txt") # 將 src.txt 復制到 dst.txt
- 移動文件:
import shutil shutil.move("src.txt", "dst.txt") # 將 src.txt 移動到 dst.txt(如果 dst.txt 存在,將被覆蓋)
- 刪除非空目錄:
import shutil shutil.rmtree("directory") # 刪除名為 "directory" 的非空目錄及其所有內(nèi)容
- 壓縮文件:
import shutil shutil.make_archive("archive", "zip", "directory") # 將 "directory" 壓縮為名為 "archive.zip" 的 ZIP 文件
- 解壓縮文件:
import shutil shutil.unpack_archive("archive.zip", "directory") # 將 "archive.zip" 解壓縮到 "directory" 目錄
以上是五個常用的 Python 標準庫模塊的簡要介紹和示例代碼。希望這些內(nèi)容可以幫助您更好地理解這些模塊的用法。請注意,這些模塊的功能遠不止這些,您可以查閱 Python 官方文檔以了解更多詳細信息。
以上就是python常用模塊(math itertools functools sys shutil)使用講解的詳細內(nèi)容,更多關(guān)于python常用模塊的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python tkinter與Mysql數(shù)據(jù)庫交互實現(xiàn)賬號登陸
本文主要介紹了python tkinter與Mysql數(shù)據(jù)庫交互實現(xiàn)賬號登陸,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01通過實例了解Python str()和repr()的區(qū)別
這篇文章主要介紹了通過實例了解Python str()和repr()的區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-01-01Python PyTorch 如何獲取 MNIST 數(shù)據(jù)
這篇文章主要介紹了Python PyTorch 如何獲取 MNIST 數(shù)據(jù),通過示例代碼介紹了PyTorch 保存 MNIST 數(shù)據(jù),PyTorch 顯示 MNIST 數(shù)據(jù)的操作方法,感興趣的朋友跟隨小編一起看看吧2024-04-04在Python中使用itertools模塊中的組合函數(shù)的教程
這篇文章主要介紹了在Python中使用itertools模塊中的組合函數(shù)的教程,來自IBM官方技術(shù)文檔,需要的朋友可以參考下2015-04-04Pycharm配置Anaconda環(huán)境的詳細圖文教程
PyCharm是一款很好用很流行的python編輯器,Anaconda通過管理工具包、開發(fā)環(huán)境、Python版本,大大簡化了你的工作流程,下面這篇文章主要給大家介紹了關(guān)于Windows系統(tǒng)下Pycharm配置Anaconda環(huán)境的相關(guān)資料,需要的朋友可以參考下2023-02-02