import time
获取现在时间
>> > import time
>> > time. localtime( )
time. struct_time( tm_year= 2020 , tm_mon= 8 , tm_mday= 21 , tm_hour= 11 , tm_min= 20 , tm_sec= 59 , tm_wday= 4 , tm_yday= 234 , tm_isdst= 0 )
>> > time. ctime( )
'Fri Aug 21 11:21:07 2020'
时间戳与计时器
>> > time. time( )
1597980231.4828305
>> > time. perf_counter( )
25.7903971
>> > time. process_time( )
0.0625
格式化输出
>> > lctime = time. localtime( )
>> > time. strftime( "%Y-%m-%d %A %H:%M:%S" , lctime)
'2020-08-21 Friday 15:21:59'
>> > time. strftime( "%Y-%m-%d" , lctime)
'2020-08-21'
>> > time. strftime( "%H:%M:%S" , lctime)
'15:21:59'
暂停
>> > time. sleep( )
import random
随机种子
>> > from random import *
>> > seed( 10 )
>> > print ( random( ) )
0.5714025946899135
>> > print ( random( ) )
0.4288890546751146
>> > seed( 10 )
>> > print ( random( ) )
0.5714025946899135
产生随机整数
>> > randint( 1 , 10 )
6
>> > randrange( 10 )
7
>> > randrange( 0 , 10 , 2 )
8
产生随机浮点数
>> > random( )
0.014832446024553692
>> > uniform( 0 , 10 )
4.625805080929491
序列用函数
>> > choice( [ 'a' , 'b' , 'c' ] )
'b'
>> > choice( "python" )
't'
>> > choices( [ 'a' , 'b' , 'c' ] , [ 4 , 4 , 2 ] , k= 20 )
[ 'b' , 'a' , 'b' , 'a' , 'a' , 'c' , 'c' , 'a' , 'c' , 'b' , 'a' , 'a' , 'b' , 'b' , 'b' , 'b' , 'a' , 'b' , 'c' , 'c' ]
>> > num = [ '1' , '2' , '3' , '4'