time模块 random模块

time模块

time、sys等模块是C语言实现的,内置到了python解释器的。而不是py文件。

导入模块的时候,优先到python解释器,然后才会找py文件。

#时间戳  #计算
# print(time.time())    #1481321748.481654秒

#结构化时间---当地时间
# print(time.localtime(1531242343))
# t=time.localtime()
# print(t.tm_year)
# print(t.tm_wday)
# #-----#结构化时间---UTC
# print(time.gmtime())

#-----将结构化时间转换成时间戳

# print(time.mktime(time.localtime()))
#------将结构化时间转成字符串时间strftime
#print(time.strftime("%Y---%m-%d %X",time.localtime()))
#------将字符串时间转成结构化时间strptime
#print(time.strptime("2016:12:24:17:50:36","%Y:%m:%d:%X"))

# print(time.asctime())
# print(time.ctime())


# import datetime
# print(datetime.datetime.now()) 
#显示的时间更符合我们要求

 random模块

import random

ret=random.random() #0-1随机浮点数  
ret=random.randint(1,3)
ret=random.randrange(1,3)
ret=random.choice([11,22,33,44,55])
ret=random.sample([11,22,33,44,55],2)#随机选两个
ret=random.uniform(1,4)#1-4的随机浮点数

print(ret)
ret=[1,2,3,4,5]
random.shuffle(ret)#打乱顺序
print(ret)

def v_code():
    ret=""
    for i in range(5):
        num=random.randint(0,9)
        alf=chr(random.randint(65,122))
        s=str(random.choice([num,alf]))
        ret+=s
    return ret
print(v_code())

原文地址:https://www.cnblogs.com/yuanchenqi/articles/5732581.html

原文地址:https://www.cnblogs.com/jiawen010/p/9811701.html