【python中的内置模块】

python内置模块

1.模块?定义的xx.py文件就是模块
2.模块的分类
通过模块的创建者:
1.系统内置模块:Python官方提供的cpython解释器提供的模块
2.第三方模块:程序员、组织、公司;第三方模块需要使用的,首先需要安装模块
在线安装(有网):使用python自带的pip命令(eg:在windows命令行pip install requests/python -m pip install requests)
离线安装:先下载离线安装包、解压安装包、安装中肯定 setup.py、以管理员身份进入cmd输入:python install setup.py
3.自定义模块
3.模块的导入问题
方法一:使用import关键字
import 模块名
方法二:使用别名
import 模块名 as 别名
方法三:使用from(强烈推荐)
from 包 import 模块名称
4.内置模块有哪些
(1).random模块:用来产生随机数(伪随机数)
import random

dir(random)
[‘betavariate’, ‘choice’, ‘choices’, ‘expovariate’, ‘gammavariate’, ‘gauss’, ‘getrandbits’, ‘getstate’, ‘lognormvariate’, ‘normalvariate’, ‘paretovariate’, ‘randint’, ‘random’, ‘randrange’, ‘sample’, ‘seed’, ‘setstate’, ‘shuffle’, ‘triangular’, ‘uniform’, ‘vonmisesvariate’, ‘weibullvariate’]
random模块中的方法:
randint(m,n) #产生随机整数[m,n]
random()#产生一个0~1内的随机数
uniform() #产生基于正态分布的随机数
choice() #在序列(seq-有序)中随机选择一个元素
(2).math模块:用于数学运算中
import math

dir(math)
[‘acos’, ‘acosh’, ‘asin’, ‘asinh’, ‘atan’, ‘atan2’, ‘atanh’, ‘ceil’, ‘copysign’, ‘cos’, ‘cosh’, ‘degrees’, ‘e’, ‘erf’, ‘erfc’, ‘exp’, ‘expm1’, ‘fabs’, ‘factorial’, ‘floor’, ‘fmod’, ‘frexp’, ‘fsum’, ‘gamma’, ‘gcd’, ‘hypot’, ‘inf’, ‘isclose’, ‘isfinite’, ‘isinf’, ‘isnan’, ‘ldexp’, ‘lgamma’, ‘log’, ‘log10’, ‘log1p’, ‘log2’, ‘modf’, ‘nan’, ‘pi’, ‘pow’, ‘radians’, ‘sin’, ‘sinh’, ‘sqrt’, ‘tan’, ‘tanh’, ‘tau’, ‘trunc’]
math模块中的方法:
ceil() #小数向上取整
floor() #小数向下取整
注意:全局函数中的round #四舍五入
e 属性 #自然常数
fabs() #求绝对值 等价于全局函数abs()
fmod() #求模
isnan()#判断数值不是数字,是数字返回False
isfinite() #判断是否无限
pi #圆周率
pow() #幂次方 等价于全局函数pow()s
sqrt() #开平方根
3.os模块:文件路径模块
import os

dir(os)
[curdir’, ‘defpath’, ‘device_encoding’, ‘devnull’, ‘dup’, ‘dup2’, ‘environ’, ‘errno’, ‘error’, ‘execl’, ‘execle’, ‘execlp’, ‘execlpe’, ‘execv’, ‘execve’, ‘execvp’, ‘execvpe’, ‘extsep’, ‘fdopen’, ‘fsdecode’, ‘fsencode’, ‘fspath’, ‘fstat’, ‘fsync’, ‘ftruncate’,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’, ‘stat_float_times’, ‘stat_result’, ‘statvfs_result’, ‘strerror’, ‘supports_bytes_environ’, ‘supports_dir_fd’, ‘supports_effective_ids’, ‘supports_fd’, ‘supports_follow_symlinks’, ‘symlink’, ‘sys’, ‘system’, ‘terminal_size’, ‘times’, ‘times_result’, ‘truncate’, ‘umask’, ‘uname_result’, ‘unlink’, ‘urandom’, ‘utime’, ‘waitpid’, ‘walk’, ‘write’]
os模块中的方法:
chdir() #改变路径
curdir #获取当前目录,返回的是相对路径(os.path.abspath(os.curdir))
chmod() #修改权限,linux
cpu_count() #返回cpu的核对应的线程数
getcwd() #返回绝对路径
getpid() #返回当前进程编号
getppid() #返回当前进程的父进程的编号
kill() #通过进程编号杀死进程
linesep #获取对应系统的换行符
listdir() #返回对应目录下的文件及文件夹,返回是一个列表
mkdir() #创建目录(文件夹),只支持一层
makedirs() #创建目录,支持多层创建
open #创建文件,等价于全局函数open()
pathsep #获取环境变量分隔符
sep #获取路径分隔符 window \ linux /
remove(path) #删除文件
removedirs() #删除(多级)目录
system() #执行终端命令
os模块中的子模块os.path中的方法:
from os import path

dir(path)
[‘all’, ‘builtins’, ‘cached’, ‘doc’, ‘file’, ‘loader’, ‘name’, ‘package’, ‘spec’, ‘_get_bothseps’, ‘_getfinalpathname’, ‘_getfullpathname’, ‘_getvolumepathname’, ‘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’, ‘splitunc’, ‘stat’, ‘supports_unicode_filenames’, ‘sys’]
abspath() #返回路径对应的绝对路径
altsep #查看python中的各种符号
basename() #获取文件名称
dirname() #获取文件所在目录
curdir #获取当前目录
exists() #判断文件或文件夹是否存在
getatime() #获取文件或文件夹的访问时间
getctime() #获取文件或文件夹的创建时间
getmtime() #获取文件或文件夹的修改时间
getsize() #获取文件的大小
isdir() #判断是否是文件夹
isfile() #判断是否是文件
isabs() #判断是否是绝对路径
join(,) #拼接路径
eg:name=“a.txt”
url=“G:/a/b/c”
path.join(usl,name)
等价于 url+os.sep+name
sep #获取路径分隔符
split() #分割路径 abspath=basename+dirname
realpath() #返回真实路径
4.sys模块:系统模块
import sys

dir(sys)
[‘displayhook’, ‘doc’, ‘excepthook’, ‘interactivehook’, ‘loader’, ‘name’, ‘package’, ‘spec’, ‘stderr’, ‘stdin’, ‘stdout’, ‘_clear_type_cache’, ‘_current_frames’, ‘_debugmallocstats’, ‘_enablelegacywindowsfsencoding’, ‘_getframe’, ‘_git’, ‘_home’, ‘_xoptions’, ‘api_version’, ‘argv’, ‘base_exec_prefix’, ‘base_prefix’, ‘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_wrapper’, ‘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’, ‘set_asyncgen_hooks’, ‘set_coroutine_wrapper’, ‘setcheckinterval’, ‘setprofile’, ‘setrecursionlimit’, ‘setswitchinterval’, ‘settrace’, ‘stderr’, ‘stdin’, ‘stdout’, ‘thread_info’, ‘version’, ‘version_info’, ‘warnoptions’, ‘winver’]
sys模块中的方法:
api_version #python的内部版本号
version() #获取python版本信息
argv #接收脚本参数的,注意第一个参数是脚本名称
copyright #输出cpython的版权信息
exit() #退出系统
getdefaultencoding() #获取默认编码,默认是utf-8
getfilesystemencoding() #获取文件系统编码,默认是utf-8
getrecursionlimit() #获取python对于递归最大层数
setrecursionlimit() #重新设置递归的最大层数
getrefcount(对象) #获取对象的引用计数,垃圾回收机制中
ls=[1,2,3]
a=ls
sys.getrefcount(ls)
结果是 3,默认就有一个
a=None
sys.getrefcount(ls)
结果是 2,释放了一个引用
getwindowsversion() #获取窗口的版本信息
5.(1).hashlib模块:hashlib所有hash操作起来一样
import hashlib

dir(hashlib)
[‘all’, ‘__builtin_constructor_cache’, ‘builtins’, ‘cached’, ‘doc’, ‘file’, ‘__get_builtin_constructor’, ‘loader’, ‘name’, ‘package’, ‘spec’, ‘_hashlib’, ‘algorithms_available’, ‘algorithms_guaranteed’, ‘blake2b’, ‘blake2s’, ‘md5’, ‘new’, ‘pbkdf2_hmac’, ‘sha1’, ‘sha224’, ‘sha256’, ‘sha384’, ‘sha3_224’, ‘sha3_256’, ‘sha3_384’, ‘sha3_512’, ‘sha512’, ‘shake_128’, ‘shake_256’]
使用步骤:1.创建算法对象(md5(16进制32位)/sha256),返回一个算法对象
注意:参数是一个字节数据
md5=hashlib.md5(“12345”.encode(“utf-8”))
2.第一种:如果不做盐值混淆(数据校验、安全检查),直接调用hexdigest()方法
md5.hexdigest()
结果:‘827ccb0eea8a706c4c34a16891f84e7b’(如果要想解密,就可在https://www.cmd5.com/md5在线解密破解进行解密)
第二种:盐值混淆(盐越复杂越好),因为hash容易碰撞破解,一般建议使用盐值混淆
md5.update(salt.encode(“utf-8”))
md5.hexdigest()
结果:‘2f2dc950c78ff20cf061393027ce19ed’

(2).hmac模块:用于加密密码:对称加密,密钥就是盐值,之后再将加密后的数据再做盐值混淆
m=hmac.new(“123456”.encode(“utf-8”),“python”.encode(“utf-8”),“MD5”)
m.hexdigest()

关于加密算法的分类:
按照算法是否可逆分为:
可逆算法
按照是否使用同一个密钥:
对称加密(解密和解密用同一种密钥): DES算法
非对称加密(加密和解密有一对密钥:公钥、私钥):rsa(使用密钥对ssh连接)
不可逆算法(hash算法)-不可逆,唯一:md5,shal1256
6.time模块
import time

dir(time)
[’_STRUCT_TM_ITEMS’, ‘doc’, ‘loader’, ‘name’, ‘package’, ‘spec’, ‘altzone’, ‘asctime’, ‘clock’, ‘ctime’, ‘daylight’, ‘get_clock_info’, ‘gmtime’, ‘localtime’, ‘mktime’, ‘monotonic’, ‘perf_counter’, ‘process_time’, ‘sleep’, ‘strftime’, ‘strptime’, ‘struct_time’, ‘time’, ‘timezone’, ‘tzname’]
asctime() #获取当前时间
localtime() #获取本地时间,方便自己完成
sleep() #
time()
strftime("%Y-%m-%d %H:%M:%S")
strptime()
s=“2019/08/03 07:35:35”
time.strptime(s,"%Y/%m/%d %H:%M:%S")

datetime 子模块
now() #获取当前时间
calendar模块
7.uuid(通用唯一识别码)模块:uuid是一个永不重复的字符串,用于文件
import uuid

dir(uuid)
[ ‘ctypes’, ‘getnode’, ‘int_’, ‘lib’, ‘libname’, ‘os’, ‘sys’, ‘uuid1’, ‘uuid3’, ‘uuid4’, ‘uuid5’]

uuid.uuid4().hex  #每次获取的uuid都不样
'fb6937f516b74b248ef6b413879c42f0'

补充:python的垃圾回收机制原理:引用计数为主,以标记清除和分代收集为辅

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

骑着蜗牛追汤圆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值