Python time和random模块

time模块

import time

time.sleep(1)#睡眠1秒

print(time.time())#时间戳 从过去的1970-01-01 00:00开始经历的秒数

print(time.localtime())#时间元组

print(time.gmtime(time.time()))#时间戳时间元组

print(time.mktime(time.gmtime()))#时间元组到时间戳

s=time.strftime("%Y-%m-%d %H:%M",time.localtime())#格式化时间元组为字符串
print(s)

s2=time.strptime("2008-9-8 20:30","%Y-%m-%d %H:%M")#格式化字符串为时间元组
print(s2)

在这里插入图片描述

random模块

import random
print(random.random())#介于0到1的随机浮点数字
print(random.randint(2,9))#介于参数之间的随机整形数字
print(random.randrange(0,100,3))#介于0到100之间的间隔为3的数字集合中的一个随机数字
print(random.uniform(3,9))#介于参数之间的随机浮点数字
s=random.choice([1,2,3,4,"字符8",'字符9',"字符100"]);#从列表或元组中随机一个元素返回
print(s)
list=['h','e','l','o']
print(random.shuffle(list))#打乱列表或元组的元素次序
print(list)

s2=random.sample(list,2)#从列表随机抽取参数个元素以列表形式返回,源列表不改变
print(s2)

在这里插入图片描述

发布了19 篇原创文章 · 获赞 26 · 访问量 2255

猜你喜欢

转载自blog.csdn.net/qzonelaji/article/details/104107176