时间模块和随机数模块

时间模块
时间戳:用秒显示的一个时间段
两个调用时间模块:1.import time 2.import datetime
一、time模块重要方法
1.time.time() #重要*:时间戳*
print(time.time())#1548748325.4074776:时间戳
2.time.clock() #cpu执行时间*
time.sleep(3)
print(time.clock())
#计算机cpu执行时间DeprecationWarning: time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead print(time.clock())
#意思是在python3.8里将被弃用
3.time.gmtime() #结构化时间*
print(time.gmtime())
#time.struct_time(tm_year=2019, tm_mon=1, tm_mday=29, tm_hour=8, tm_min=10, tm_sec=30, tm_wday=1, tm_yday=29, tm_isdst=0)
4.time.localtime() #计算机时间*
print(time.localtime())
#time.struct_time(tm_year=2019, tm_mon=1, tm_mday=29, tm_hour=16, tm_min=18, tm_sec=33, tm_wday=1, tm_yday=29, tm_isdst=0)
5.time.strftime() #字符串时间*
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.
6.time.strptime() #把time.strftime()类时间转化成结构化时间
print(time.strptime('2019--01--29 16:28:04','%Y--%m--%d %H:%M:%S'))
#time.struct_time(tm_year=2019, tm_mon=1, tm_mday=29, tm_hour=16, tm_min=28, tm_sec=4, tm_wday=1, tm_yday=29, tm_isdst=-1)
a=time.strptime('2019--01--29 16:28:04','%Y--%m--%d %H:%M:%S')
print(a.tm_year)#单独取出年份
print(a.tm_mday)#单独取出日
7.time.ctime() #打印当前时间(格式是固定的无法更改,括号中可以加参数)*
print(time.ctime())
print(time.ctime(123456))#参数加一个时间戳
8.time.mktime() #参数加结构化时间*
print(time.mktime(time.localtime()))#把当前时间转化为时间戳(秒)
二、表示时间三种方式:1.时间戳 2.结构化时间 3.格式化时间
三、datetime模块:
datetime.now()#现在时间*
print(datetime.datetime.now())
随机数模块
调用随机数模块:import random
1.random.random()#0-1间的随机数
print(random.random())
2.random.rand()#可规定范围的随机整数
print(random.randint(1,8))#1-8间的随机整数(包括8)
3.random.choice('hello')#范围内随机字符或序列
print(random.choice('hello'))
print(random.choice(['123',4,[1,2]]))
4.random.sample()#随机选多个
print(random.sample(['123',4,[1,2],4,5],2))#序列中随机选两个
3.random.randrange()#范围内随机生成,不包含末尾
print(random.randrange(1,3))#不包含3
例:
方法一:(有缺陷)
import random
#print(random.sample(['123',4,[1,2],4,5],2))
#print(random.randrange(1,3))#不包含3
def yzm():
c = ''
for i in range(5):
if i==random.choice(0,5):
add=random.randrange(10)
else:
add=chr(random.randrange(65,91))
c+=str(add)
print(c)
yzm()
方法二:(完整简单)
import random
#print(random.sample(['123',4,[1,2],4,5],2))
#print(random.randrange(1,3))#不包含3
def yzm():
c = ''
for i in range(5):
add=random.choice([random.randrange(10),chr(random.randrange(65,91))])
# if i==random.choice(0,5):
# add=random.randrange(10)
# else:
# add=chr(random.randrange(65,91))
c+=str(add)
print(c)
yzm()
posted @ 2019-01-29 18:01  HashFlag  阅读(357)  评论(0)    收藏  举报