栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python基础—内置模块 2021-11-30

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python基础—内置模块 2021-11-30

Python内置模块

一,math模块
常用方法:
【‘ceil’, ‘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’】

|-- ceil # 向上取整
|-- floor # 向下取整
|-- math.e # 自然常数
|-- math.pi # pi
|-- fabs # 取绝对值
|-- pow # 几的几次方
|-- sqrt # 开平方根

二,uuid模块:
uuid.uuid4().hex
UUID的字符串,该字符串是不重复字符串
注:常用UUID4
三,random模块
计算机中的随机数都是伪随机数
【‘betavariate’, ‘choice’, ‘choices’, ‘expovariate’, ‘gammavariate’,
‘gauss’, ‘getrandbits’, ‘getstate’, ‘lognormvariate’, ‘normalvariate’,
‘paretovariate’, ‘randint’, ‘random’, ‘randrange’, ‘sample’, ‘seed’,
‘setstate’, ‘shuffle’, ‘triangular’, ‘uniform’, ‘vonmisesvariate’, ‘weibullvariate’】

|-- randint(start, end) # [start, end]区间范围内的随机整数
|-- random() # [0, 1)区间范围内的随机数
|-- choice(seq) # 随机从容器中拿表
|-- randrange(start, end) # [start, end]区间范围内的随机整数
|-- shuffle(seq) # 洗牌
|-- uniform(start, end) # [start, end]区间范围内的随机数

四,os
操作系统相关模块,操作操作系统中非常重要,文件系统
【‘abc’, ‘abort’, ‘access’, ‘add_dll_directory’, ‘altsep’, ‘chdir’, ‘chmod’, ‘close’, ‘closerange’, ‘cpu_count’, ‘curdir’, ‘defpath’, ‘device_encoding’, ‘devnull’, ‘dup’, ‘dup2’, ‘environ’, ‘error’, ‘execl’, ‘execle’, ‘execlp’, ‘execlpe’, ‘execv’, ‘execve’, ‘execvp’, ‘execvpe’, ‘extsep’, ‘fdopen’, ‘fsdecode’, ‘fsencode’, ‘fspath’,‘fstat’,‘fsync’‘ftruncate’,‘get_exec_path’, ‘get_handle_inheritable’, ‘get_inheritable’, ‘get_terminal_size’, ‘getcwd’, ‘getcwdb’, ‘getenv’, ‘getlogin’, ‘getpid’, ‘getppid’, ‘isatty’, ‘kill’, ‘linesep’, ‘link’, ‘listdir’, ‘lseek’, ‘lstat’, ‘makedirs’, ‘mkdir’, ‘name’, ‘open’, ‘pardir’, ‘path’, ‘pathsep’, ‘pipe’, ‘popen’, ‘putenv’, ‘read’, ‘readlink’, ‘remove’, ‘removedirs’,‘rename’, ‘renames’, ‘replace’, ‘rmdir’, ‘scandir’, ‘sep’, ‘set_handle_inheritable’,‘set_inheritable’ ‘spawnl’, ‘spawnle’, ‘spawnv’, ‘spawnve’, ‘st’, ‘startfile’, ‘stat’, ‘symlink’, ‘sys’, ‘system’, ‘terminal_size’, ‘times’, ‘times_result’, ‘truncate’, ‘umask’, ‘uname_result’, ‘unlink’, ‘urandom’, ‘utime’, ‘waitpid’, ‘walk’, ‘write’】

|-- chdir(path) # 修改当前的工作空间
|-- chmod # 修改文件权限
|-- cpu_count() # 返回CPU的架构信息
|-- curdir # 获取当前工作目录
|-- device_encoding # 设备编码
|-- getcwd() # 打印当前路径
|-- getpid() # 当前进程编号
|-- getppid() #
|-- listdir([path]) # 列表
|-- os.sep # 分割目录
|-- Linux命令,一定要练习下

五,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) # 返回绝对路径
|-- basename(path) # 文件名称
|-- dirname(path) # 文件的主目录
|-- exists(path) # 判断文件存不存在
|-- getatime(path) #
|-- getctime # 获取创建时间
|-- getmtime # 获取修改时间
|-- getsize # 获取文件大小
|-- join(path1, path2) # 拼接路径
|-- split(path) # 分割

六,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’, ‘modules’, ‘path’, ‘path_hooks’, ‘path_importer_cache’, ‘platform’, ‘prefix’, ‘ps1’, ‘ps2’, ‘pycache_prefix’, ‘set_asyncgen_hooks’, ‘setprofile’, ‘setrecursionlimit’, ‘setswitchinterval’, ‘settrace’, ‘stderr’, ‘stdin’, ‘stdout’, ‘thread_info’, ‘unraisablehook’, ‘version’, ‘version_info’, ‘warnoptions’, ‘winver’】

|-- api_version # 获取API版本
|-- argv # 获取脚本参数,注意,以列表形式保存脚本
|-- exit() # 退出系统
|-- getdefaultencoding() # 获取系统(python解释器)的默认编码
|-- getfilesystemencoding() # 获取操作系统的文件系统的默认编码
|-- getrecursionlimit() # python解释器,对于递归的调用次数,限制为1000次
|-- setrecursionlimit() # 设置递归从层数
|-- getrefcount() # 获取变量的引用计数

七,time
【‘altzone’, ‘asctime’, ‘ctime’, ‘daylight’, ‘get_clock_info’, ‘gmtime’, ‘localtime’, ‘mktime’, ‘monotonic’, ‘monotonic_ns’, ‘perf_counter’, ‘perf_counter_ns’, ‘process_time’, ‘process_time_ns’, ‘sleep’,‘strftime’, ‘strptime’, ‘struct_time’, ‘thread_time’, ‘thread_time_ns’, ‘time’, ‘time_ns’, ‘timezone’, 'tzname】

|-- time.asctime() # 获取当前时间(返回字符串)
|-- time.ctime() # 获取当前时间
|-- time.gmtime([seconds]) # 获取时间对象(UTC)
|-- time.localtime([seconds]) # 获取时间对象(本地时间)
|-- sleep(seconds) # 休眠()秒
|-- time.time() # 时间戳|邮戳 时间对象和数值之间的关系
|-- strftime # 格式化时间对象为特定格式的字符串
|-- strptime # 从字符串转换为时间对象
|-- struct_time() # 构造时间戳对象

datetime:
datetime.datetime

  • |-- now() # 获取当前时间
  • |-- …… # 获取(年,月,日,星期,时,秒,分 )

calendar模块: 日历

八,hashlib

IT领域的加密算法:
|-- 不可逆加密(准确来说不是加密算法、摘要算法)
hash算法(比如说求模)
特点:
单向的,不可逆的明文(原生数据) ====> 密文(加密后的数据)
结果是确定的
将任何数据映射为一个数据
……
md5 # 必须要掌握
sha256

    |-- 可逆加密
	明文(原生数据)	<====>		密文(加密后的数据)
	|-- 对称加密
		加密和解密使用同一个秘钥(key)
		DES
	|-- 非对称加密
		加密和解密使用的不同的秘钥(一对秘钥)
		RSA

hashlib
hash加密的内置库

如果做数据完整性的校验:不要混淆  md5
如果要加密数据:要求一定要做盐值混淆!!!!

如何完成盐值混淆:

  • update方法,本质就是在后面追加了数据(数据越长、复杂)
九,IO流

什么是IO流:
指的是,计算机中数据在内存中流入流出的技术在狭义上,主要指的是,内存到磁盘(存储设备) 在广义上,也包括网络数据的传输。
python对于IO流,就是open函数
IO流的分类:
数据的流动方向:
|-- 输入流
|-- 输出流
数据的类型:
|-- 字节流
|-- 字符流
open函数:
第一个参数:表示文件路径或者文件名称(该文件必须存在于工作空间)

第二个参数:
	open:
	r: 读取数据
	b: 字节数据
	W:写数据(覆盖)
	a: 写数据(追加)
	t: 字符数据(默认)
	close() # 关闭
	open("a.txt","rt",encoding="utf-8")
	read()  # 读取数据
	open("b.txt","wt")
	write() # 写入数据
	flush() # 强行刷新
十,对象序列化

对象序列化:
对象:抽象概念
如果我们要传输对象,就需要将对象由逻辑概念转换为真实存在的数据(字符串、字节)
对象反序列化:
将物理状态的数据转换为逻辑状态

python提供了两个对象序列化和反序列化的模块
pickle:对象 <> 字节数据
json: 对象 <
> json
pickle模块:

pickle.dump(obj, file):对象持久化(先序列化,在持久化)
pickle.load(file):反持久化,将数据从磁盘重新读成对象
picke.dumps(obj):对象序列化
pickle.loads(bytes):反序列化对象

json模块:

ison.dump(obj, file):对象持久化(先序列化,在持久化)
ison.load(file):反持久化,将数据从磁盘重新读成对象
ison.dumps(obj):对象序列化
ison.loads(bytes):反序列化对象

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/618942.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号