random模組time模組

2021-10-25 18:02:29 字數 1442 閱讀 3485

import time

time.time(

)# 從2023年到現在,一共過了多少秒

# 一般這麼用

t1 = time.time(

)# code running time to be count

t2 = time.time(

)total = t2 - t1

time.strftime(

"%y"

)# 2020 年

time.strftime(

"%y"

)# 有待查證,應該是年分後兩位

time.strftime(

"%m"

)# 月份

time.strftime(

"%m"

)# 分鐘

time.strftime(

"%d"

)# date 10/12/20 月/日/年

time.strftime(

"%d"

)# day

# 同理,h 表示小時,m 分鐘, s 秒

# a 本地簡化星期 a 本地完整星期

# b 簡化月份英文輸出(哎,雅思考試,9月份忘記怎麼拼寫了,好傷) b 完整月份輸出

# j 年內第幾天,比如2020-10-12是第286天

# w 表示星期幾,其中星期天是0,w 第幾個星期

# x 本地日期, x 本地時間

time.localtime(t1)

# 結構化時間,是乙個元組,有興趣的讀者可以在cmd裡面去看看長啥樣子

# 其實如果得到乙個t1 = time.time,可以轉換成格式話的時間格式,也可以轉換回去

time.mktime(t1)

# 轉換回去

import random

random.random(

)# 大於0且小於1的小數

random.random(1,

3)# 大於1且小於3的小數

random.randint(1,

5)# 大於1且小於5的整數

random.randrange(1,

10,2)

# 大於1且小於10之間的奇數(也即是可以被)

random.randrange(2,

11,2)

# 2-11之間,每兩個取乙個,從中挑選乙個整數 (2, 11, 5)模擬

random.choice(

['1',2

],'aaa'

)# 隨機選擇返回

random.sample([1

,'aaa',[

1,2]

,'xyp'

], 2

)# 任意兩個組合成列表後返回

random.shuffle(

list

)# 打亂,但沒有返回值

time模組 random模組

匯入模組的時候,優先到python直譯器,然後才會找py檔案。時間戳 計算 print time.time 1481321748.481654秒 結構化時間 當地時間 print time.localtime 1531242343 t time.localtime print t.tm year p...

random模組與time包

名稱 說明random 隨機time 時間import random x random.random y random.random print x,100 y 0.22526585028408974 90.33529228969773import random m random.randint 0...

Python之random模組和time模組

import random x random.random y random.random print x,y 10 random.random 隨機生成乙個 0,1 之間的隨機數 m random.randint 0,10 print m random.randint 隨機生成乙個 0 10 之間...