1. 程式人生 > >python語法_模組_time_datetime_random

python語法_模組_time_datetime_random

模組:可以被公共呼叫的。

time

import time

print(time.time()) 時間戳方式顯示時間。

time.sleep(3) 休眠3秒

time.clock() 計算cpu執行時間

time.gmtime() 結構化時間

time.locatime() 本地化時間

struct_time = time.localtime()

print(time.strftime('%Y--%m--%d %H:%M:%S',struct_time)) 字串時間

print(time.strptime('2018--12--10 15:11:05','%Y--%m--%d %H:%M:%S')) 轉換成結構化時間

a = time.striptime('2018--12--10 15:11:05','%Y--%m--%d %H:%M:%S')

a.tm_year,a.tm_mday,a.tm_wday

print(time.ctime()) ctime加引數的話,會計算出時間戳再顯示當前時間

print(time.mktime(time.localtime()))  計算時間戳秒數

######

datetime模組

import datatime

print(datetime.datetime.now())

 

 

########

random模組 #隨機數模組

import random

print(random.random()) 0-1之間隨機生成一個隨機數

print(random.randint(1,8)) 1-8之間隨機成成一個整數,包括8

print(random.choice('asdasdasd') 字串隨機生成一個

print(random.sample(['asd','1',asd,2]) 從序列裡隨機選取2個

 

print(random.randrange(1,3)) 隨機取1個數,不包含3