今天学习完内置模板,来总结总结,给大家分享一下我的总结。
那我也就不废话了,直接进入今天的主题——“内置模块”
1、导入模块
import random(插入模块)
我们一般使用import关键字导入模块。
from 包 import 模块名称 (推荐这种格式!)
2、random模块
#0,1之间时间生成的浮点数 float
>>>**random.random()**
>>>0.02892244733244087
#随机生成传入参数范围内的数字 即 1,2,3
>>>**random.randint(0,5)**
>>>4
#在序列(seq)随机选择任意一个数字
>>>a = [1,2,3,4,5,6]
>>>**random.choice(a)**
>>>6
#产生基于正态分布的随机数
>>>**random.uniform(1,10)**
>>>7.443019821764108
#对列表进行打乱顺序。
>>>a = [1,2,3,4,5]
>>>**random.shuffle(a)**
>>>a
>>>[3, 4, 1, 2, 5,]
3、math模块
1、ceil #对数值进行向上取整
>>>math.ceil(10.0000001)
>>>11
2、floor #对数值进行向下取整
>>>math.floor(10.0000001)
>>>10
3、round #全局函数中四舍五入
>>>round(4,5)
>>>4
>>>round(5,5)
>>>6
4、e #取自然常数
>>>math.e
>>>2.718281828459045
4、e #取自然常数
>>>math.e
>>>2.718281828459045
5、pi #取圆周率
>>>math.pi
>>>3.141592653589793
6、fabs #取绝对值(等价于全局函数abs)
>>>math.fabs(-10)
>>>10
7、fmod #取模
>>>math.fmod(13,7)
>>>6.0
8、isnan() #判断某个数值不是数值
>>>math.isnan(123)
>>>Flase
9、pow #幂次方(等价于全局函数pow)
>>>math.pow(3,3)
>>>27
10、sqrt #开平方根
>>>math.sqrt(9)
>>>3
4、sys模块
1、api_version # python的内部版本号
>>>sys.api_version
>>>1013
2、argv() # 接收脚本参数的,注意第一个参数是脚本名称
>>>import sys
>>>print(sys.argv) #接收脚本参数
3、copyright # 输出cpython的版权信息
>>>sys.copyright
>>>'Copyright (c) 2001-2018 Python Software
Foundation.
4、sys.exit() # 退出系统
>>>sys.exit()
>>>
5、getdefaultencoding() # 获取默认编码,默认是utf-8
>>>sys.getdefaultencoding()
>>>'utf-8'
6、 getfilesystemencoding() # 获取文件系统默认编码,默认是utf-8
>>>sys.getfilesystemencoding()
>>>'utf-8'
7、getrecursionlimit() # 获取python对于递归的限制层数
>>>sys.getrecursionlimit()
>>>1000
8、setrecursionlimit(num) # 重新设置递归的限制层数
9、getrefcount(对象) # 获取对象的引用计数,垃圾回收机制中
>>>a = [1,2,3,4,5]
>>>sys.getrefcount(a)
2
10、getwindowsversion # 获取窗口的版本信息
>>>sys.getwindowsversion()
>>>sys.getwindowsversion(major=10, minor=0, build=18362, platform=2, service_pack='')
11、version() #获取版本信息
>>>sys.version
>>>'3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)]'
5、hashlib库
1、hashlib所有hash操作起来一样(注意)
2、使用步骤
**1、创建算法对象(md5 sha256),返回一个算法对象**
#注意:参数值字节数据
md5 = hashlib.md5("12345".encode("utf-8"))
**2、如果不做盐值混淆,直接调用hexdigest()方法**
md5.hexdigest()
**3、盐值混淆**
hash容易碰撞破解,一般建议使用盐值混淆
md5.update(salt)
3、hamc
也是一个哈希加密库,而且使用对称加密
首先会使用对称加密(秘钥就是盐值),之后将加密后的数据再做一次hash加密,盐值混淆
所以整个结果十分安全
6、time模块
在Python中,通常有这几种方式来表示时间:
1、时间戳(timestamp) :通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。我们运行“type(time.time())”,返回的是float类型。
2、格式化的时间字符串
3、元组(struct_time) :struct_time元组共有9个元素共九个元素:(年,月,日,时,分,秒,一年中第几周,一年中第几天,夏令时)
1、 asctime() # 获取当前时间
>>> import time
>>> time.asctime()
'Sun Mar 29 18:31:08 2020'
2、 ctime() # 获取当前时间
>>> time.ctime()
'Sun Mar 29 18:31:18 2020'
3、 localtime() # 本地时间,方便于自己完成格式化
>>> time.localtime()
time.struct_time(tm_year=2020, tm_mon=3, tm_mday=29, tm_hour=18, tm_min=31, tm_sec=27, tm_wday=6, tm_yday=89, tm_isdst=0)
4、 sleep(1) # 休眠时间,单位是秒
>>> time.sleep(4)
5、 time() # 获取当前时间戳
>>> time.time()
1585478058.3027372
6、strptime # 将一个特定格式的时间字符串转换为时间对象
>>> a ="2020-3-29 8:59:42"
>>> time.strptime(a,"%Y-%m-%d %H:%M:%S")
time.struct_time(tm_year=2020, tm_mon=3, tm_mday=29, tm_hour=8, tm_min=59, tm_sec=42, tm_wday=6, tm_yday=89, tm_isdst=-1)
7、 strftime # 将一个时间对象格式化为特定的字符串
>>> time.strftime("%Y-%m-%d")
'2020-03-29'
7、datatime模块
import datetime #插入模块
1、#显示当前时间日期
print(datetime.datetime.now())
# 2020-3-29 16:10:36.019032
sad
2、#当前时间+3天
print(datetime.datetime.now() + datetime.timedelta(3))
#2018-09-23 16:10:36.020031
8、uuid (通用唯一识别码)
特点:永不重复的字符串
uuid.uuid4().hex
>>> uuid.uuid4().hex
'09b0e2bf482d4b9196cff7648cc3d533'
>>> uuid.uuid4().hex
'711ae4b1f25a4de19fac8d88c551b9be'
>>> uuid.uuid4().hex
'da87cc2f24824f94aba470e8d8ca2f72'
今天的总结就到这里了,我么明天见!
加油!同志们 奥里给!!!