sys模块
sys.argv获得启动时的参数sys.path获取加载的环境sys.exit()程序退出time模块
time模块,包含和时间相关信息time.time()获取当前时间戳,单位是秒,(相对于1970年1月1日的0点0时0分0秒)import timeresult = time.time() # 获取当前时间print(result)
time.sleep(秒)阻断程序 ```python import time
print(‘程序开始’)
睡眠3秒钟
time.sleep(3)
print(‘程序结束’)
<a name="pKsC5"></a>## datetime模块`datetime`获取日期<br />`datetime.datetime.now().year`<br />`datetime.datetime.now().month`<br />`datetime.datetime.now().day````pythonimport datetime# 获取当前年year = datetime.datetime.now().year# 获取月month = datetime.datetime.now().month# 获取日期day = datetime.datetime.now().dayprint(year)print(month)print(day)# 格式化日期时间now = datetime.datetime.now()str_time = datetime.datetime.strftime(now, "%Y-%m-%d %H:%M:%S")
计算排序
aaa = [2, 4, 1, 2, 3, 6]# 列表最大值print(max(aaa))# 列表最小值print(min(aaa))# 列表和print(sum(aaa))# 列表长度print(len(aaa))# 列表排序print(sorted(aaa))# 列表倒序print(sorted(aaa, reverse=True))
math模块
math是数学相关的库
math.pow 求幂math.floor 取下math.ceil 取上round 四舍五入math.sin cos tan…
import math# 幂print(math.pow(10, 2))# 向下取整print(math.floor(1.8234234234234))# 向上取整print(math.ceil(1.1234234234234))# 四舍五入print(round(1.6234234234234))# sin 传入弧度值 pi 3.14 180度print(math.sin(1.57))
5. random模块
random主要用来产生随机数
random.randint(start,end) # 随机整数,[start,end]random.random() # 随机浮点数, [0,1)random.uniform(start,end) # 随机浮点数, [start,end]random.choice([]) # 随机数组, 返回单个元素random.choices([]) # 随机数组, 返回列表
import random# 随机整数print(random.randint(10, 20))# 随机小数 [0, 1)print(random.random())# 随机浮点类型数据print(random.uniform(1.3, 8.5))# 从列表中随机获取元素lst = [10,20,30]print(random.choice(lst))# 随机返回列表 返回单个元素的列表print(random.choices(lst))
