Python相關(guān)模塊介紹
一、Python引用擴(kuò)展模塊
? import <模塊>[as <別名>]:將模塊中的函數(shù)等名稱導(dǎo)入當(dāng)前程序,“命名空間”namespace,引用方法——<模塊>.<名稱>
? dir(<名稱>)函數(shù):列出名稱的屬性
? help(<名稱>)函數(shù):顯示參考手冊(cè)
? from <模塊>import <名稱> :導(dǎo)入模塊的部分名稱

二、時(shí)間相關(guān)模塊
1.calendar模塊
? 跟日歷相關(guān)的若干函數(shù)和類,可以生成文本形式的日歷
? calendar.calendar(<年>)
? calendar.month(<年>,<月>),返回多行字符串
? calendar.isleap(<年>),判別閏年
? calendar.prmonth(<年>,<月>)
? calendar.prcal(<年>)
?

2.datetime模塊
? 有4個(gè)主要的類:date處理年月日,time處理時(shí)分秒、毫秒,datetime處理日期加時(shí)間,timedelta處理時(shí)段(時(shí)間間隔)
? 常用函數(shù)/方法:datetime.date.today(),datetime.datetime.now(),datetime.datetime.isoformat()
? 兩個(gè)時(shí)間相減就是timedelta


3.time模塊
? time.time()浮點(diǎn)數(shù)表示的現(xiàn)在時(shí)間:從1970-1-1 0:0:0開始的秒數(shù)
?time.struct_time結(jié)構(gòu)化時(shí)間類:time.localtime(<紀(jì)元時(shí)間>)->結(jié)構(gòu),time.gmtime(<紀(jì)元時(shí)間>)->結(jié)構(gòu),time.mktime(<結(jié)構(gòu)化時(shí)間>)->紀(jì)元時(shí)間
? time.strftime(<格式>)表示格式化輸出(結(jié)構(gòu)化)時(shí)間
? time.strptime(<字串>,<格式>)按照格式識(shí)別字串,返回時(shí)間

三、算數(shù)模塊
? math:常用的算術(shù)函數(shù)、三角函數(shù)、冪指數(shù)等等
? cmath:支持復(fù)數(shù)的math函數(shù)
? decimal:十進(jìn)制定點(diǎn)數(shù),十進(jìn)制小數(shù),不再有浮點(diǎn)數(shù)的誤差
? fractions:有理數(shù),比例,進(jìn)行分?jǐn)?shù)運(yùn)算


? random:隨機(jī)數(shù)。
random.randint(a,b),random.randrange(start,stop,step) ,
random.choice(seq),random.sample(seq,n)。
? statistics:一些統(tǒng)計(jì)函數(shù)。
平均值:mean,中位數(shù):median,標(biāo)準(zhǔn)偏差:stdev/pstdev。



四、持久化:shelve
? 將任何數(shù)據(jù)對(duì)象,保存到文件中去
? 類似字典形式訪問(wèn),可讀可寫:import shelve,f = shelve.open(<文件名>),f[key] = value,value = f[key],del f[key],f.close()。


公眾號(hào)推薦:數(shù)據(jù)思踐
數(shù)據(jù)思踐公眾號(hào)記錄和分享數(shù)據(jù)人思考和踐行的內(nèi)容與故事。
《數(shù)據(jù)科學(xué)與人工智能》公眾號(hào)推薦朋友們學(xué)習(xí)和使用Python語(yǔ)言,需要加入Python語(yǔ)言群的,請(qǐng)掃碼加我個(gè)人微信,備注【姓名-Python群】,我誠(chéng)邀你入群,大家學(xué)習(xí)和分享。
關(guān)于Python語(yǔ)言,有任何問(wèn)題或者想法,請(qǐng)留言或者加群討論。
