Time Module
Time is divided into three types: timestamp, structured time, format time
#Time module,Import Time#time Stampx =time.time () time.gmtime ( )#convert timestamps to UTC time Tuplesy = Time.localtime ()#Convert a timestamp to a time-tuple in a time zonePrint(y)#structured data, in the form of tuplesy = time.mktime (y)#convert structured data to timestampsPrint(y)#Formatting Dataz = time.strftime ("%y-%m-%d%h:%m:%s", y)#convert structured data to formatted data#time.strftime ("format", "Structured time data (tuple)")---> Convert structured time data to formatted time data#time.strptime ("Format time string", "format")-----> Match formatted time string in a given format and convert to formatted time dataExample#>>> x = time.localtime ()#>>> Print (x)#time.struct_time (tm_year=2017, tm_mon=7, tm_mday=9, tm_hour=23, tm_min=11, Tm_se#c=32, Tm_wday=6, tm_yday=190, tm_isdst=0)#>>> time.strftime ("%y-%m-%d%h:%m:%s", x) #即%Y to match tm_year,%m match Tm_mon without attention to order#' 2017-07-09 23:11:32 '#>>>#>>> time.strptime (' 2017-07-09 23:11:32 ', "%y-%m-%d%h:%m:%s")%Y match 2017, must be noted in order#time.struct_time (tm_year=2017, tm_mon=7, tm_mday=9, tm_hour=23, tm_min=11, Tm_se#c=32, Tm_wday=6, tm_yday=190, Tm_isdst=-1)##time.ctime (x) #将时间戳数据转换成特定格式#>>> time.ctime (x)#' Mon Jul 19:26:16 '#>>> time.asctime (y) #将结构化数据转换成特定格式#' Mon Jul 19:26:45 'Time Module
Time plus minus
ImportdatetimePrint(Datetime.datetime.now ())#back to 2016-08-19 12:47:03.941925Print(Datetime.date.fromtimestamp (Time.time ()))#The timestamp is converted directly into the date format 2016-08-19Print(Datetime.datetime.now ())Print(Datetime.datetime.now () + Datetime.timedelta (3))#Current time + 3 daysPrint(Datetime.datetime.now () + Datetime.timedelta (-3))#Current Time-3 daysPrint(Datetime.datetime.now () + Datetime.timedelta (hours=3))#Current time + 3 hoursPrint(Datetime.datetime.now () + Datetime.timedelta (minutes=30))#current time +30 minutesC_time =Datetime.datetime.now ()Print(C_time.replace (minute=3,hour=2))#Time Substitution
Time conversion diagram
Random module
1 ImportRandom2 #Print (Random.randint (1,9))3 #print (Random.random ())4 #5 #Print (Random.randrange (2,10,4))6 #Print (random.sample (' Chenglv ', 2))7 #Print (Random.randrange (0,99,2))8 #Print (Random.uniform (1,10))Generate verification CodeTenCheckcode ="" One forIinchRange (4): Ax = Random.randint (0, 9) -y = Random.choice ("ADBCDEFGLIKOPNM") - ifi = = Random.randint (0,3): theTMP =x - Else: -TMP =y -Checkcode + =STR (TMP) + - Print(Checkcode)
Python---built-in modules