常用模块
time模块
-
在Python中,通常有这几种方式来表示时间:1)时间戳 2)格式化的时间字符串 3)元组(struct_time)共九个元素。由于Python的time模块实现主要调用C库,所以各个平台可能有所不同。
-
UTC(Coordinated Universal Time,世界协调时)亦即格林威治天文时间,世界标准时间。在中国为UTC+8。DST(Daylight Saving Time)即夏令时。
-
时间戳(timestamp)的方式:通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。我们运行“type(time.time())”,返回的是float类型。返回时间戳方式的函数主要有time(),clock()等。
-
元组(struct_time)方式:struct_time元组共有9个元素,返回struct_time的函数主要有gmtime(),localtime(),strptime()。
datetime模块
datetime是python处理日期和时间的标准库。
random模块
import random # 随机生成0-1的float数 print(random.random())#0.48548102849510477 #随机1,2,3三个int数 print(random.randint(1,3))#3 #随机1,2两个int数 print(random.randrange(1,3))#2 #随机1,23,[4,5] print(random.choice([1,'23',[4,5]]))#[4,5] #根据第二个参数指定的组合和数 随机第一个列表 print(random.sample([1,'23',[4,5]],2))#[1, '23'] #大于1小于3的float数 print(random.uniform(1,3))#1.469061065732845 #相当于将item打乱重组 item=[1,3,5,7,9] random.shuffle(item) print(item) #[3, 9, 5, 7, 1] # 生成随机验证码 def make(): res='' for i in range(5): a=chr(random.randint(65,90)) b=str(random.randint(0,9)) res+=random.choice([a,b]) return res print(make())