Python3 time模块&datetime模块&random模块
1 ''' 2 time模块 3 ''' 4 5 # import time 6 # print(help(time)) # help()提供帮助 7 # print(time.time()) # 1970年开始到现在的秒数(时间戳)——重点 8 # time.sleep(3) # CPU不工作,阻塞——重点 9 # print(time.clock()) # 计算CPU工作时间 10 # 11 # # 结构化(元组形式) 12 # print(time.gmtime()) # 格里尼治天文台时间,标准时间——重点 13 # # time.struct_time(tm_year=2017, tm_mon=10, tm_mday=18, tm_hour=1, tm_min=56, tm_sec=47, tm_wday=2, tm_yday=291, tm_isdst=0) 14 # print(time.localtime()) # 本地时间——重点 15 # a1 = time.localtime() 16 # print(time.strftime('%Y-%m-%d %H:%M:%S',a1)) # 自定义时间显示样式——重点 17 # print(time.strptime('2017-09-08 10:21:10','%Y-%m-%d %H:%M:%S')) # 转换为结构化时间,前后的格式要一一对应,各市要相同 18 # # 重点 19 # print(time.ctime()) # 当前时间(格式不能动),参数默认为空,加入参数后,将参数转换为时间,从1970年算起 20 # print(time.mktime(time.localtime())) # 转化为时间戳 21 22 ''' 23 datetime 模块 24 ''' 25 # import datetime 26 # print(datetime.datetime.now()) # 显示现在时间 27 28 29 30 31 ''' 32 random 模块 33 ''' 34 # import random 35 # 36 # print(random.random()) # 随机生成数,random()——默认范围是0-1 37 # print(random.randint(1,8)) # 范围1-8 38 # print(random.choice('hello')) # 随机选择 39 # print(random.choice([123,'asd',[1,2]])) # 随机选择元素 40 # # print(random.sample([123,'asd'],1,44)) # TypeError: sample() takes 3 positional arguments but 4 were given 41 # print(random.sample([123,'asd'],1)) # 正确 42 # print(random.randrange(1,5)) # range 取值取不到5,范围1-4 43 # 44 # 45 # # 验证码函数 46 # # version--1 47 # def rand_code(): 48 # code = '' 49 # 50 # for i in range(5): 51 # if i == random.randint(0,4): 52 # add = random.randrange(10) 53 # else: 54 # add = chr(random.randrange(65,91)) 55 # code += str(add) 56 # print(code) 57 # rand_code() 58 # 59 # # version--2 60 # def fun1(): 61 # code = '' 62 # for i in range(5): 63 # add = random.choice([random.randrange(10),chr(random.randrange(65,91))]) 64 # code += str(add) 65 # print(code) 66 # fun1()