Python常用的模塊非常多,主要分為內(nèi)置模塊和第三方模塊兩大類(lèi),且不同模塊應(yīng)用場(chǎng)景不同又可以分為文本類(lèi)、數(shù)據(jù)結(jié)構(gòu)類(lèi)、數(shù)學(xué)運(yùn)算類(lèi)、文件系統(tǒng)類(lèi)、爬蟲(chóng)類(lèi)、網(wǎng)絡(luò)通訊類(lèi)等多個(gè)類(lèi)型。
大家常用的內(nèi)置模塊比如:math、re、datetime、urllib、os、random等,第三方模塊比如pandas、numpy、requests、matplotlib等。
什么是Python模塊?
模塊是將復(fù)雜的、同一應(yīng)用領(lǐng)域的功能代碼進(jìn)行封裝,你只需要調(diào)用接口,輸入相應(yīng)參數(shù),便可以輕松拿到結(jié)果,類(lèi)似瑞士軍刀、萬(wàn)能工具箱。

常用內(nèi)置模塊,約200多個(gè)
內(nèi)置模塊,顧名思義就是Python軟件內(nèi)嵌的模塊,無(wú)需額外安裝。
想要了解詳細(xì)的內(nèi)置模塊,最好去Python官網(wǎng)看,挺詳細(xì)的
https://docs.python.org/zh-cn/3/library/index.html
你也可以在代碼行輸入print(help(modules)),會(huì)顯示全部的內(nèi)置模塊

這里舉幾個(gè)常用的內(nèi)置模塊,并附上代碼:
math 模塊
用來(lái)進(jìn)行數(shù)學(xué)計(jì)算,它提供了很多數(shù)學(xué)方面的專(zhuān)業(yè)函數(shù),適合科研、算法
import math
# 計(jì)算平方根
sqrt_value = math.sqrt(25)
print("Square Root:", sqrt_value)
# 計(jì)算正弦值
sin_value = math.sin(math.radians(30))
print("Sine Value:", sin_value)
re 模塊
正則表達(dá)式在Python中的擴(kuò)展實(shí)現(xiàn),該模塊能支持正則表達(dá)式幾乎所有語(yǔ)法,對(duì)于文本處理來(lái)說(shuō)必不可少
import re
# 查找匹配的字符串
pattern = r"\d+"
text = "There are 123 apples and 456 oranges."
matches = re.findall(pattern, text)
print("Matches:", matches)
datetime 模塊
用于處理日期和時(shí)間,這個(gè)模塊非常實(shí)用?。?!
import datetime
# 獲取當(dāng)前日期和時(shí)間
current_datetime = datetime.datetime.now()
print("Current Date and Time:", current_datetime)
# 格式化日期時(shí)間
formatted_datetime = current_datetime.strftime("%Y-%m-%d %H:%M:%S")
print("Formatted Date and Time:", formatted_datetime)
urllib 模塊
用于進(jìn)行網(wǎng)絡(luò)請(qǐng)求,獲取網(wǎng)頁(yè)HTML,所謂的爬蟲(chóng)就是這個(gè)模塊
import urllib.request
# 發(fā)起HTTP GET請(qǐng)求
response = urllib.request.urlopen("https://www.example.com")
html = response.read()
print("HTML Content:", html[:100])
os 模塊
提供了與操作系統(tǒng)交互的功能,比如文件和目錄操作
import os
# 獲取當(dāng)前工作目錄
current_dir = os.getcwd()
print("Current Directory:", current_dir)
# 列出目錄中的文件和子目錄
files_and_dirs = os.listdir(current_dir)
print("Files and Directories:", files_and_dirs)
random 模塊
用于生成偽隨機(jī)數(shù)
import random
# 生成隨機(jī)整數(shù)
random_integer = random.randint(1, 10)
print("Random Integer:", random_integer)
# 從列表中隨機(jī)選擇元素
random_element = random.choice(["apple", "banana", "cherry"])
print("Random Element:", random_element)
json 模塊
專(zhuān)門(mén)用來(lái)處理 JSON 格式數(shù)據(jù)
import json
# 將字典轉(zhuǎn)換為 JSON 格式的字符串
data = {"name": "Alice", "age": 25}
json_string = json.dumps(data)
print("JSON String:", json_string)
# 將 JSON 格式的字符串轉(zhuǎn)換為字典
parsed_data = json.loads(json_string)
print("Parsed Data:", parsed_data)
collections 模塊
提供了一些除list、dict之外有用的數(shù)據(jù)容器,比如 defaultdict、Counter 等
from collections import defaultdict, Counter
# 創(chuàng)建默認(rèn)字典
word_counts = defaultdict(int)
words = ["apple", "banana", "apple", "cherry", "banana", "apple"]
for word in words:
word_counts[word] += 1
print("Word Counts:", word_counts)
# 統(tǒng)計(jì)元素出現(xiàn)的次數(shù)
element_counts = Counter(words)
print("Element Counts:", element_counts)
csv 模塊
專(zhuān)門(mén)用于處理逗號(hào)分隔值(CSV)文件
import re
# 查找匹配的字符串
pattern = r"\d+"
text = "There are 123 apples and 456 oranges."
matches = re.findall(pattern, text)
print("Matches:", matches)
sys 模塊
提供了與Python解釋器交互的功能,例如訪問(wèn)命令行參數(shù)
import sys
# 獲取命令行參數(shù)
arguments = sys.argv
print("Command-line Arguments:", arguments)
常用的第三方模塊,十幾萬(wàn)個(gè)
Python之所以這么受歡迎,很大一部分原因得益于強(qiáng)大的第三方工具生態(tài),幾乎各個(gè)領(lǐng)域都有對(duì)應(yīng)的模塊可以使用。
比如
數(shù)據(jù)科學(xué)領(lǐng)域:pandas、numpy、scipy、sympy
可視化領(lǐng)域:matplotlib、seaborn、plotly、bokeh、pyecharts
機(jī)器學(xué)習(xí)領(lǐng)域:scikit-learn、keras、Tensorflow
大數(shù)據(jù)領(lǐng)域:pyspark、pyflink
爬蟲(chóng)領(lǐng)域:requests、scrapy、bs4
金融量化領(lǐng)域:ta-lib、zipline、pyfolio
其他各領(lǐng)域都有相應(yīng)的模塊可以使用,這里就不一一列舉

總得來(lái)說(shuō),Python常用的模塊非常多,還是要根據(jù)你的使用場(chǎng)景來(lái)選擇,大家可以去Python官網(wǎng)、github上找相應(yīng)的模塊及教程。