偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

我常用的幾個經(jīng)典Python模塊

開發(fā) 后端
Python常用的模塊非常多,還是要根據(jù)你的使用場景來選擇,大家可以去Python官網(wǎng)、Github上找相應的模塊及教程。

Python常用的模塊非常多,主要分為內(nèi)置模塊和第三方模塊兩大類,且不同模塊應用場景不同又可以分為文本類、數(shù)據(jù)結(jié)構(gòu)類、數(shù)學運算類、文件系統(tǒng)類、爬蟲類、網(wǎng)絡通訊類等多個類型。

大家常用的內(nèi)置模塊比如:math、re、datetime、urllib、os、random等,第三方模塊比如pandas、numpy、requests、matplotlib等。

什么是Python模塊?

模塊是將復雜的、同一應用領域的功能代碼進行封裝,你只需要調(diào)用接口,輸入相應參數(shù),便可以輕松拿到結(jié)果,類似瑞士軍刀、萬能工具箱。

常用內(nèi)置模塊,約200多個

內(nèi)置模塊,顧名思義就是Python軟件內(nèi)嵌的模塊,無需額外安裝。

想要了解詳細的內(nèi)置模塊,最好去Python官網(wǎng)看,挺詳細的。

https://docs.python.org/zh-cn/3/library/index.html。

你也可以在代碼行輸入print(help(modules)),會顯示全部的內(nèi)置模塊。

這里舉幾個常用的內(nèi)置模塊,并附上代碼:

「math 模塊」

用來進行數(shù)學計算,它提供了很多數(shù)學方面的專業(yè)函數(shù),適合科研、算法。

import math

# 計算平方根
sqrt_value = math.sqrt(25)
print("Square Root:", sqrt_value)

# 計算正弦值
sin_value = math.sin(math.radians(30))
print("Sine Value:", sin_value)

「re 模塊」

正則表達式在Python中的擴展實現(xiàn),該模塊能支持正則表達式幾乎所有語法,對于文本處理來說必不可少。

import re

# 查找匹配的字符串
pattern = r"\d+"
text = "There are 123 apples and 456 oranges."
matches = re.findall(pattern, text)
print("Matches:", matches)

「datetime 模塊」

用于處理日期和時間,這個模塊非常實用!

import datetime

# 獲取當前日期和時間
current_datetime = datetime.datetime.now()
print("Current Date and Time:", current_datetime)

# 格式化日期時間
formatted_datetime = current_datetime.strftime("%Y-%m-%d %H:%M:%S")
print("Formatted Date and Time:", formatted_datetime)

「urllib 模塊」

用于進行網(wǎng)絡請求,獲取網(wǎng)頁HTML,所謂的爬蟲就是這個模塊。

import urllib.request

# 發(fā)起HTTP GET請求
response = urllib.request.urlopen("https://www.example.com")
html = response.read()
print("HTML Content:", html[:100])

「os 模塊」

提供了與操作系統(tǒng)交互的功能,比如文件和目錄操作。

import os

# 獲取當前工作目錄
current_dir = os.getcwd()
print("Current Directory:", current_dir)

# 列出目錄中的文件和子目錄
files_and_dirs = os.listdir(current_dir)
print("Files and Directories:", files_and_dirs)

「random 模塊」

用于生成偽隨機數(shù)。

import random

# 生成隨機整數(shù)
random_integer = random.randint(1, 10)
print("Random Integer:", random_integer)

# 從列表中隨機選擇元素
random_element = random.choice(["apple", "banana", "cherry"])
print("Random Element:", random_element)

「json 模塊」

專門用來處理 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)建默認字典
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)計元素出現(xiàn)的次數(shù)
element_counts = Counter(words)
print("Element Counts:", element_counts)

「csv 模塊」

專門用于處理逗號分隔值(CSV)文件。

import re

# 查找匹配的字符串
pattern = r"\d+"
text = "There are 123 apples and 456 oranges."
matches = re.findall(pattern, text)
print("Matches:", matches)

「sys 模塊」

提供了與Python解釋器交互的功能,例如訪問命令行參數(shù)。

import sys

# 獲取命令行參數(shù)
arguments = sys.argv
print("Command-line Arguments:", arguments)

常用的第三方模塊,十幾萬個

Python之所以這么受歡迎,很大一部分原因得益于強大的第三方工具生態(tài),幾乎各個領域都有對應的模塊可以使用。

比如

  • 數(shù)據(jù)科學領域:pandas、numpy、scipy、sympy
  • 可視化領域:matplotlib、seaborn、plotly、bokeh、pyecharts
  • 機器學習領域:scikit-learn、keras、Tensorflow
  • 大數(shù)據(jù)領域:pyspark、pyflink
  • 爬蟲領域:requests、scrapy、bs4
  • 金融量化領域:ta-lib、zipline、pyfolio

其他各領域都有相應的模塊可以使用,這里就不一一列舉。

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

責任編輯:姜華 來源: 今日頭條
相關推薦

2011-05-23 16:50:12

python

2022-07-19 10:02:49

Vueuse文檔

2010-03-22 15:38:46

Python常用模塊

2018-06-08 11:02:43

編程語言Python模塊

2020-10-29 10:59:44

Python開發(fā)數(shù)據(jù)

2010-07-14 15:52:28

Telnet命令

2010-03-12 15:13:41

Python常用模塊

2021-05-06 15:15:13

Python工具代碼

2020-02-23 23:29:07

Python編程開發(fā)

2025-01-22 10:33:44

Python大模型

2024-05-28 00:00:10

Python模塊開發(fā)

2010-03-22 10:27:28

Python常用模塊I

2010-10-13 09:45:50

Linux監(jiān)控腳本

2009-03-23 10:25:22

JavaOracle應用開發(fā)

2022-05-22 07:29:24

工具插件客戶端軟件

2024-11-07 11:10:34

Python腳本統(tǒng)計分析

2024-03-01 13:48:00

Git配置系統(tǒng)

2023-11-30 07:37:49

MySQL函數(shù)

2015-07-14 10:13:29

2022-04-12 08:43:21

Python內(nèi)置模塊
點贊
收藏

51CTO技術(shù)棧公眾號