1. 程式人生 > 其它 >python常見內建模組

python常見內建模組

技術標籤:內建模組python

常見內建模組:
1.什麼是內建模組:
就是py檔案
2.如何使用模組:
匯入模組:

->import 模組名稱	    #最常見的匯入方式
->imoport 模組名稱 as 新名稱   #匯入時重新命名
->from 模組名稱 import 方法名稱/子模組/類   as 新名字
->from 模組名稱 import *#*統配

3.內建模組:
官方已經將這些模組實現了,已經內建了Cpython
random模組:
隨機數模組,計算機中的隨機數都是偽隨機數。

->choice(seq)    #隨機從一個序列中選出某個元素
->randint(a,b)		#隨機整數[a,b]
->random()			#[0,1)隨機數
->rangdrange()
->shuffle()			#洗牌
->uniform()			#隨機實數[a,b]			

choice(seq):
在這裡插入圖片描述
randint(a,b):
在這裡插入圖片描述
random():
在這裡插入圖片描述
rangdrange():
在這裡插入圖片描述
shuffle() :
在這裡插入圖片描述
uniform():
在這裡插入圖片描述
math模組:

'acos', 'acosh', 'asin', 'asinh', 'atan','atan2'
 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 
 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 
 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum',
 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite',
 'isinf', 'isnan', 'isqrt', 'ldexp', 'lgamma', 'log', 
 'log10', 'log1p', 'log2', 'modf', 'nan', 'perm',
 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin',
 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc'

->三角函式相關的:

'acos', 'acosh', 'asin', 'asinh', 'atan','atan2'
 'atanh'

->ceil #向上取整
->floor#向下取整
->fab#
->pow#冪次方
->sqrt開平方根
ceil() , floor()
在這裡插入圖片描述
pow(),sqrt():
在這裡插入圖片描述
sys模組:
該模組主要的作用就是對python直譯器系統進行各種操作。

'addaudithook', 'api_version', 'argv', 'audit',
'base_exec_prefix', 'base_prefix', 'breakpointhook', 'builtin_module_names', 'byteorder',
'call_tracing', 'callstats', 'copyright',
'displayhook', 'dllhandle', 'dont_write_bytecode', 
’exc_info', 'excepthook', 'exec_prefix',
'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'get_asyncgen_hooks',
'get_coroutine_origin_tracking_depth', 'getallocatedblocks', 'getcheckinterval',
'getdefaultencoding', 'getfilesystemencodeerrors', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'getswitchinterval', 'gettrace', 'getwindowsversion', 'hash_info', 'hexversion', 'implementation', 
'int_info', 'intern', 'is_finalizing',
'last_traceback', 'last_type', 'last_value', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1', 'ps2', 'pycache_prefix', 'set_asyncgen_hooks', 'set_coroutine_origin_tracking_depth', 'setcheckinterval', 'setprofile', 'setrecursionlimit', 
'setswitchinterval', 'settrace', 'stderr', 'stdin',
'stdout', 'thread_info', 'unraisablehook', 'version', 'version_info', 'warnoptions', 'winver'

->sys.api_version#獲取python核心程式碼版本號
->argv()#獲取指令碼引數
->exit()#退出系統
->getdefaultencoding()#獲取預設編碼
->getfilesystemencoding()#獲取系統編碼
->getrefcount()#獲取引用計數的個數
->sys.path#外掛模組路徑集合
->setrecursionlimit() #設定遞迴的限制層數
->stdin#標準輸入流
->stout#標準輸出流
argv:
#剩餘的都是執行指令碼引數,注意:引數以空格相隔。
在這裡插入圖片描述
getdefaultencoding()#獲取預設編碼
getfilesystemencoding()#獲取系統編碼
在這裡插入圖片描述
getrecursionlimit()#獲取遞迴的限制層數,預設1000
在這裡插入圖片描述
getrefcount()#獲取引用計數的個數
引用計數法,通過引用的數量判斷統計記憶體是否為有用的記憶體,還是垃圾記憶體(通常指堆中),看棧中有沒有引用堆中的記憶體,若有則為有用。
在這裡插入圖片描述
OS 模組:
主要用來作業系統檔案的系統(files ystem)
‘remove’, ‘removedirs’, ‘rename’, ‘renames’, ‘replace’, ‘rmdir’, ‘scandir’, ‘sep’,‘system’
->chdir()#修改工作空間
->getcwd()#返回當前工作空間,相對路徑
->curdir()#返回當前工作空間,絕對路徑
->cpu_count()#獲取cpu的執行緒數
->getpid()#獲取當前的程序編號
->getppid()#獲取當前程序的父程序編號
->listdir()#返回當前工作空間的所有檔案及資料夾的名稱
->makedirs()#新建資料夾,可以級聯建立
->mkdir()#新建資料夾,不可以級聯建立
->remove(path|檔名稱)#刪除檔案
->removedirs()#可以級聯刪除資料夾
->rename#修改文建名稱
->scandir#等同於listdir,但是返回接果是一個迭代器
->sep#路徑分隔符
->system#執行系統的命令
os.path:

'abspath', 'altsep', 'basename', 'commonpath',
 'commonprefix', 'curdir', 'defpath', 
'devnull', 'dirname', 'exists', 'expanduser',
'expandvars', 'extsep', 'genericpath', 'getatime',
'getctime', 'getmtime', 'getsize', 'isabs', 'isdir',
'isfile', 'islink', 'ismount', 'join', 'lexists', 'normcase',
'normpath', 'os', 'pardir', 'pathsep', 'realpath',
'relpath', 'samefile', 'sameopenfile', 'samestat', 'sep',
'split', 'splitdrive', 'splitext', 'stat', 'supports_unicode_filenames', 'sys

|-abspath(path)#返回絕對路徑
|–altsep
|–basename#檔名稱
|–dirname#檔案的主目錄
|–exitsts()#判斷是否存在
|–join()#路徑拼接
|–split()