time 模块 和 random 模块常用方法讲解

import  time
print(help(time))
print(time.time())#时间戳 1573991312.5361328
print(time.perf_counter())#计算CPU的执行时间
结构化时间:print(time.gmtime())#UTC时间 世界标准时间time.struct_time结构化时间(tm_year=2019, tm_mon=11, tm_mday=17, tm_hour=12, tm_min=0, tm_sec=39, tm_wday=6, tm_yday=321 , tm_isdst=0)元组这是个
结构化时间:print(time.localtime()) #本地时间time.struct_time(tm_year=2019, tm_mon=11, tm_mday=17, tm_hour=20, tm_min=5, tm_sec=33, tm_wday=6, tm_yday=321, tm_isdst=0)
格式化时间:print(time.strftime('%Y--%m--%d %H:%M:%S',time.localtime()))#转换为字符串时间自定义格式2019--11--17 20:13:30
格式化时间:print(time.strptime('2019--11--17 20:13:30','%Y--%m--%d %H:%M:%S'))#转换为结构化时间time.struct_time(tm_year=2019, tm_mon=11, tm_mday=17, tm_hour=20, tm_min=13, tm_sec=30, tm_wday=6, tm_yday=321, tm_isdst=-1)
b = time.strptime('2019--11--17 20:13:30','%Y--%m--%d %H:%M:%S')
print(b.tm_year) #将得到的结构化时间元素单个取出
print(b.tm_yday)
print(b.tm_wday)
print(time.ctime())#等价于print(time.asctime()) 运行结果Sun Nov 17 20:32:41 2019 #表示当前时间sunday nomenclature 17日
print(time.ctime(3600))#参数表示在1970年0时开始所加的秒数 运行结果print(time.ctime()) #表示过了所加时间后的时间
print(time.mktime(time.localtime()))#将结构化时间转换为时间戳1573994354.0
import datetime
print(datetime.datetime.now())#2019-11-17 20:43:26.441406表示当前时间与之前的格式化时间相近


import random
print(random.random()) #随机得到一个大于零小于一的数如0.30384291289332777
print(random.randint(1,9))#随机得到一个一到九的数包含1和9即左右都边包含
print(random.randrange(1,3))#随机取一个1到3的数取不到3 即右边取不到 *这个用到较多

print(random.choice('ilovedengmei'))
print(random.choice(['ioo','www',[21212]])) #将choice里面的整体中的某个元素随机取出来

a=random.randint(1,3)
print(random.sample(['ioo','www',[21212]],a)) #随机在左边的序列中选a个出来重新排列成一个新序列

#生成5位数随机验证码
def c_ode():
code = ''
for i in range(5):
c_ode_num = random.randrange(1,10)
code +=str(c_ode_num)
print(code)
c_ode()

随机生成带字母的5位验证码 方法一
print(chr(65)) #将编码表的第65位取出即A
print(chr(90)) #将编码表的第90位取出即Z
def c_ode():
code = ''
for i in range(5):
add = random.randrange(1, 10)
add = chr(random.randrange(65,91))
if i==random.randrange(4): #这个方法会导致验证码数字太少
add = random.randrange(1, 10)
else:
add = chr(random.randrange(65, 91))

code +=str(add)

print(code)
c_ode()


随机生成带字母的5位验证码 方法二
def c_ode():
auth_code=''
for i in range(5):
add = random.choice([random.randrange(0,10),chr(random.randrange(65,91))])
auth_code += str(add)
print(auth_code)
c_ode()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

a small tree

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值