Time Module
ImportTime#Introducing Time ModulesPrint(Time.time ())#1508146954.9455004: Time stampPrint(Time.clock ())#Calculate CPU Execution TimePrint(Time.gmtime ())#UTC TimePrint(Time.localtime ())#local timePrint(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 fromUTC.%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.Print(Time.strftime ("%y-%m-%d%h:%m:%s%z") ) execution results:2017-10-16 18:17:39Fansik= Time.strptime ('2017-10-16 18:16:26',"%y-%m-%d%h:%m:%s")Print(Fansik) Execution Result: Time.struct_time (tm_year=2017, tm_mon=10, tm_mday=16, tm_hour=18, tm_min=16, tm_sec=26, tm_wday=0, tm_yday=289, Tm_isdst=-1)Print(fansik.tm_year) execution results:2017ImportdatetimePrint(Datetime.datetime.now ()) Implementation results:2017-10-16 18:27:03.256569
Random module
ImportRandomPrint(Random.random ())#random number from 0 to 1:0.06054267487515341Print(Random.randint (1, 9999))#random numbers from 1 to 9999, including 1 and 9999Print(Random.randrange (1, 9999))#random number from 1 to 9999, not including 9999Print(Random.choice ('Fansik'))#a character in a random FansikPrint(Random.choice (['Fansik','Fanjinbao','Zhangsan']))#random of these three namesPrint(Random.sample (['Fansik','Zhangsan','Fanjinbao','Lisi'], 2))#randomly selected two namesRandom Verification Code:ImportRandomdefv_code (): Code="' forIinchRange (5): Add= Random.choice ([Random.randrange], Chr (Random.randrange (65, 91))]) #if i = = Random.randint (0, 4): #add = Random.randrange (Ten) #Else: #add = Chr (Random.randrange ()Code + =Str (ADD)Print(Code) V_code ()
Python Basics-time modules and Radom modules