python0基礎學習之常用模組

2021-10-07 01:43:10 字數 3978 閱讀 6254

模組(module),它特指就是***.py檔案
如果預設沒有在包中

import ***x import random

import uuid, sys

import os.path as 別名

from 包路徑 import 模組名稱

from 包路徑.模組名稱 import 方法、類、變數

from functools import partial

random:

目前所有的計算機api實現的隨機都是偽隨機數

|-- random.random() # 返回乙個[0, 1)的隨機數

|-- random.randint(n, m) # 返回[n, m]範圍內的隨機整數

|-- random.randrange(n, m) # 返回[n, m)範圍內的隨機整數

|-- random.choice(sequence) # 從乙個序列中隨機返回乙個元素

|-- random.uniform(n, m) # 返回[n, m]範圍內的隨機數

|-- random.shuffle(列表) # 隨機洗牌列表中的元素順序

math:

進行數學計算

|-- math.ceil(num) # 向上取整

|-- math.floor(num) # 向下取整

|-- math.abfs(num) # 求絕對值

|-- math.modf(num) # 以元組的形式返回整數和小數部分

|-- math.pi # 圓周率

|-- math.pow(num, num2) # 冪次方 num**num2

|-- math.sqrt(num) # 開平方根

os:

主要管理作業系統檔案管理系統

|-- chdir(path) # 修改當前工作空間路徑的

|-- curdir # 當前工作空間的路徑

|-- os.getcwd() # 獲取路徑的絕對路徑

|-- os.path.abspath(path) # 獲取路徑的絕對路徑

|-- os.cpu_count() # 獲取當前電腦的cpu架構

|-- os.device_encoding(fd) # 獲取裝置的編碼

|-- os.getpid() # 獲取程序編號

|-- os.getppid() # 獲取當前程序的父程序編號

|-- os.kill() # 通過編號殺死程序

|-- os.listdir(path) # 遍歷path路徑下的所有檔案和資料夾,以列表形式返回名稱

|-- os.mkdir() # 建立乙個單層資料夾(目錄)

|-- os.makedirs() # 建立多層目錄

|-- os.name # 獲取作業系統名稱

|-- os.rename() # 重新命名

|-- os.remove() # 移除檔案

|-- os.removes() # 移除多層目錄

|-- os.rmdir() # 刪除目錄

|-- os.sep # 當前系統對應的檔案分隔符

os.path

推薦使用 from os import path 匯入方式

|-- os.path.abspath(path) # 獲取path的絕對路徑

|-- os.path.altsep # 路徑的分割符

|-- os.path.basename(path) # 獲取路徑對應的檔名稱

|-- os.path.dirname(path) # 獲取檔案所在目錄

|-- os.path.exists(path) # 判斷檔案是否存在

|-- os.path.getatime(path) # 獲取檔案最後的訪問時間

|-- os.path.getctime(path) # 獲取檔案的建立時間

|-- os.path.getmtime(path) # 獲取檔案的修改時間

|-- os.path.getsize(path) # 獲取檔案大小,單位是b(位元組)

|-- os.path.isdir(path) # 判斷path是不是目錄

|-- os.path.isfile(path) # 判斷路徑是不是乙個檔案

|-- os.path.isabs(path) # 判斷路徑是不是乙個絕對路徑

|-- os.path.islink(path) # 判斷路徑是不是乙個鏈結(軟連線、硬鏈結)

|-- os.path.ismount(path) # 判斷路徑是不是乙個掛載檔案(u盤、光碟機等)

|-- os.path.join(path1, path2) # 完成路徑的拼接,會自動根據系統生成分隔符

|-- os.path.split(path) # 分隔檔案路徑

time:

|-- time.asctime()			# 返回當前時間,格式是西歐格式

|-- time.ctime() # 返回當前時間,格式是西歐格式

|-- time.gmtime() # 返回當前時間,時間物件,注意:時區是倫敦時區

|-- time.localtime() # 返回當前時間,時間物件,注意:時區是當前時區

|-- time.sleep(seconds) # 讓當前執行緒停止執行

|-- time.time() # 獲取當前時間戳

|-- time.struct_time(元組) # 構造乙個對應的時間物件

|-- time.strftime() # 將乙個時間物件格式化為特定的字串輸出

|-- time.strptime() # 將特定的字串時間轉換為時間物件

datetime模組

import datetime

|-- datetime.datetime.now() # 獲取當前時間物件

|-- datetime.date(year, month, day) # 構造乙個日期物件

|-- datetie.time(hour, min, seconds) # 獲取時間物件

|-- str(datetime物件) # 格式化datetime物件為字串

calendar模組

日曆模組
hashlib模組

加密演算法相關的模組

加密演算法:

|-- 通過是否可逆

|-- 可逆加密

|-- 加密和解密是否使用同乙個秘鑰

|-- 對稱加密 des

使用同一秘鑰加密和解密

|-- 非對稱加密 rsa

使用一對公私鑰對

|-- 不可逆加密(雜湊加密、hash加密、雜湊加密)

md3、md4、md5

>>> md5 = hashlib.md5("123".encode("utf-8"))

>>> md5.hexdigest()

'202cb962ac59075b964b07152d234b70'

sha1、sha256、sha384 ...

二進位制的128位陣列 ==> 32位字串

使用場景:密碼加密、檔案校驗

hash,引入密值 salt

uuid:具有唯一性

import uuid

s = uuid.uuid4()

s.hex

今天分享內容就到這裡,給大家分享幾個練習題。

1、遍歷磁碟

輸入乙個路徑,列印當前路徑及子路徑中的所有檔案的絕對路徑

3、完成乙個登陸註冊案例(密碼加密)

練習題參考

python 0基礎學習筆記14 爬蟲

爬取網頁 import urllib.request 向指定的url位址發起請求,並返回伺服器響應的資料 檔案物件 response urllib.request.urlopen 讀取檔案的全部內容,會把讀取到的資料賦值給乙個字串變數 data response.read decode utf 8 ...

python 0基礎入門課筆記

參加了一次入門的體驗課,對我來說內容是比較淺的,但是課程設計得確實用心,值得為此付費 程式語言是為和機器溝通,所以,先要讓機器能與你對話,python裡讓機器說話的語句是 print 從名字不難理解,機器說話其實就是把內容列印到螢幕上的過程,至於說啥呢,就是括號裡的內容 那如何表達說的內容呢?高考給...

Python 0基礎 第二章 python語言基礎

我的課本學習筆記 1.拼接字串,前後需要都轉成字串的格式,則不需要 money all 56.75 72.91 88.50 26.37 68.51 累加總計金額 money all str str money all 轉換為字串 print 商品總金額為 money all str print 商品...