常见内置模块:
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()
原文链接:https://blog.csdn.net/m0_47218990/article/details/113180119