常用模块-------时间模块(time/datetime),随机数模块(random)

#三种时间表示方法:时间戳,结构化时间,格式化时间
import time
import datetime
import random

#************************************************时间模块(time,datetime)**************************************************

# print(time.time())#时间戳 从1970年1月1日到当前时间的总秒数 也被称为Unix时间戳 1526198309.0440397

# print(time.sleep(3))#cpu停止工作的时间,具体时间由sleep中的参数决定,此时为3秒

# print(time.clock())#计算cpu执行的时间 8.924722641932095e-07

# print(time.localtime())#当地时间 结构化时间
#[time.struct_time(tm_year=2018, tm_mon=5, tm_mday=13, tm_hour=15, tm_min=59, tm_sec=52, tm_wday=6, tm_yday=133, tm_isdst=0)]

# localtime=time.localtime()
# print(time.strftime('%Y--%m--%d %H:%M:%S',localtime))#把结构化时间转化成字符串(格式化)时间 2018--05--13 15:53:29

#print(time.strptime('2012-3-4 12:24:34','%Y-%m-%d %H:%M:%S'))#把字符串(格式化)时间转化成结构化时间
# time.struct_time(tm_year=2012, tm_mon=3, tm_mday=4, tm_hour=12, tm_min=24, tm_sec=34, tm_wday=6, tm_yday=64, tm_isdst=-1)
# a=time.strptime('2012-3-4 12:24:34','%Y-%m-%d %H:%M:%S')
# print(a.tm_hour)#12
# print(a.tm_year)#2012

# print(time.ctime())#把秒转化成从1970年1月1日计数的具体时间,默认为当前时间 Sun May 13 16:22:25 2018
# print(time.ctime(3600))#Thu Jan 1 09:00:00 1970

# localtime=time.localtime()
# print(time.mktime(localtime))#把具体时间转化成秒 1526199884.0

#print(datetime.datetime.now())#用datetime模块表示的当前时间 2018-05-13 16:44:47.720970

#**********************************************随机数模块(random)************************************************************

# print(random.randint(1,7))#随机生成一个1~7的整数

# print(random.choice([[1,2],'a',8]))#随机生成参数序列中的任一元素

#print(random.sample([[1,2],'a',8],2))#随机生成参数序列中的任2个元素(具体随机生成几个元素由后一个参数决定)

# print(random.randrange(1,7))#随机生成一个1~6的整数

#-------应用之生成一个5位验证码---------
def v_code():
code=''
for i in range(5):
add=random.choice([random.randrange(10),chr(random.randrange(65,91))])
code+=str(add)
print(code)

v_code()
go go go! ! !
原文地址:https://www.cnblogs.com/zbooo/p/9032749.html