One: Iterator 2 & Adorner
#Codeing:utf-8#__author__:duke#date:2018/3/10/010#today's talk about Iterators#generators are iterators, iterators are not necessarily generatorsL = [1,2,3,4]data=iter (L)Print(data)#<list_iterator object at 0x000002abbf1e9780>#What is an iterator? 1 have ITER Method 2 have next method#practice finding the longest line in a fileFile = Open ("Data","R", encoding="UTF-8") Maxlength_data="'defmax_line (): whileTrue:data_line=file.readline (). Strip ()Print(Data_line)if notData_line: Break GlobalMaxlength_dataifLen (data_line) >Len (maxlength_data): Maxlength_data=Data_lineElse: Pass yield forIinchmax_line (): NonePrint("==========")Print(Maxlength_data) file.close ()
Two: Time module
#Time ModuleImport TimePrint(Time.time ())#gets the current time stamp * * * *Print(Help (Time))#get help for a modulePrint(Time.clock ())#calculate the CPU execution timePrint(Time.sleep (3))#Sleep time *******Print(Time.gmtime ())#UTC World Standard Time #time. Struct_time (tm_year=2018, tm_mon=3, tm_mday=10, tm_hour=12, tm_min=6, tm_sec=56, tm_wday=5, Tm_yda y=69, tm_isdst=0)#UTC World Standard TimePrint(Time.localtime ())#local timeStruct_time =time.localtime ()Print(Time.strftime ("%y-%m-%d%h:%m:%s", Struct_time))#formatted output of timePrint(Time.strptime ('2016-09-08 18:48:35','%y-%m-%d%h:%m:%s'))#Assigning a time to a variable#It is also a time-structuredPrint(Time.ctime ())#cannot customize the way the format gets the current timePrint(Time.ctime (0))#verbose time for printing timestampsPrint(Time.mktime (Time.localtime ()))#structured time is converted to timestamp
Three: Random module
#Codeing:utf-8#__author__:duke#date:2018/3/14/014 19:29ImportRandomPrint(Help (Random))Print(Random.random ())#print a random number in [0.1)Print(Random.randint (1,8))#including the number on the rightPrint(Random.choice ('Hello'))#randomly select characters in a stringPrint(Random.choice (['123', 45,'OK']))#randomly select characters in a stringPrint(Random.choices (['123', 45,'OK', 3,4,5,7]))#Print(Random.sample (['123', 45,'OK', 3,4,5,7],2))#randomly select multiple numbersPrint(Random.randrange (1,7))#does not include the last number
#random number generation using stochastic moduledefv_code (): Code="' forIinchRange (6): x= Random.choice ([a]) ifx = = 1: Code_num= Random.randrange (0,10) Code+=Str (code_num)elifx = = 2: Code_small_word= Random.randrange (65,91) Code+=chr (Code_small_word)Else: Code_big_word= Random.randrange (97,123) Code+=chr (Code_big_word)returnCodePrint(V_code ())
Python Learning Day11