Python基础-时间模块和radom模块
2017-10-17 09:24
483 查看
时间模块
import time # 引入时间模块 print(time.time()) # 1508146954.9455004: 时间戳 print(time.clock()) # 计算CPU执行时间 print(time.gmtime()) # UTC时间 print(time.localtime()) # 本地时间 print(time.strftime("%Y-%m-%d %H:%M:%S")) %Y Year with century as a decimal number. %m Month as a decimal number [01,12]. %d Day of the month as a decimal number [01,31]. %H Hour (24-hour clock) as a decimal number [00,23]. %M Minute as a decimal number [00,59]. %S Second as a decimal number [00,61]. %z Time zone offset from UTC. %a Locale's abbreviated weekday name. %A Locale's full weekday name. %b Locale's abbreviated month name. %B Locale's full month name. %c Locale's appropriate date and time representation. %I Hour (12-hour clock) as a decimal number [01,12]. %p Locale's equivalent of either AM or PM. print(time.strftime("%Y-%m-%d %H:%M:%S%z")) 执行结果: 2017-10-16 18:17:39 fansik = time.strptime('2017-10-16 18:16:26',"%Y-%m-%d %H:%M:%S") print(fansik) 执行结果: time.struct_time(tm_year=2017, tm_mon=10, tm_mday=16, tm_hour=18, tm_min=16, tm_sec=26, tm_wday=0, tm_yday=289, tm_isdst=-1) print(fansik.tm_year) 执行结果: 2017 import datetime print(datetime.datetime.now()) 执行结果: 2017-10-16 18:27:03.256569
random模块
import random print(random.random()) # 0到1的随机数:0.06054267487515341 print(random.randint(1, 9999)) # 1到9999的随机数,包含1和9999 print(random.randrange(1, 9999)) # 1到9999的随机数,不包含9999 print(random.choice('fansik')) # 随机fansik中的一个字符 print(random.choice(['fansik', 'fanjinbao', 'zhangsan'])) # 随机这三个名字 print(random.sample(['fansik', 'zhangsan', 'fanjinbao', 'lisi'], 2)) # 随机选两个名字 随机验证码: import random def v_code(): code = '' for i in range(5): add = random.choice([random.randrange(10), chr(random.randrange(65, 91))]) # if i == random.randint(0, 4): # add = random.randrange(10) # else: # add = chr(random.randrange(65, 91)) code += str(add) print(code) v_code()
相关文章推荐
- python基础回顾---时间模块
- python基础-OrderedDict、defaultdict、Counter、时间模块、sys模块
- Python基础操作 文件的读写 异常模块 类面向对象 时间日历
- python基础===时间处理模块
- python---基础之模块,列表,元组,字典
- 2.python语法基础-模块
- python基础--import模块导入
- 【Python基础】08、Python模块和包
- Python-基础-时间日期处理小结
- python基础-random、os模块、异常处理模块
- python基础===monkeytype可以自动添加注释的模块!
- python datetime模块处理时间
- Python学习笔记(三十九)— 内置模块(8)XML基础
- python 时间模块备忘
- 【python】logging模块基础
- python常用的时间处理模块time
- python 时间模块小结
- Python时间time模块介绍
- python基础---python中常用的模块
- Python笔记 - 时间就是金钱,datetime模块的详解