争取写完,持续更新
一.sys
1.识别操作系统
import sys
print(sys.pltfrom)
二.os
三.math
四.random
五.time
1.time函数用于返回当前的时间戳(格林尼治时间起至现在的总秒数)
import time
now=time.time()
print(now)
1611303060.773287
2.localtime函数用于将时间戳格式转化为本地时间,返回(struct_time)对象
import time
now=time.localtime()
print(now)
time.struct_time(tm_year=2021, tm_mon=1, tm_mday=22, tm_hour=16, tm_min=14, tm_sec=30, tm_wday=4, tm_yday=22, tm_isdst=0)
3.mktime函数接收struct_time返回用秒数表示时间的浮点数
import time
now=time.localtime()
print(now)
s=time.mktime(now)
print(s)
time.struct_time(tm_year=2021, tm_mon=1, tm_mday=22, tm_hour=16, tm_min=21, tm_sec=1, tm_wday=4, tm_yday=22, tm_isdst=0)
1611303661.0
4.gmtime函数可以将时间戳转化为0时区的struct_time
import time
now=time.time()
print(now)
s=time.gmtime(now)
print(s)
1611303823.960081
time.struct_time(tm_year=2021, tm_mon=1, tm_mday=22, tm_hour=8, tm_min=23, tm_sec=43, tm_wday=4, tm_yday=22, tm_isdst=0)
5.asctime函数可以接收struct_time返回可读的时间形式
import time
now=time.time()
print(now)
s=time.gmtime(now)
print(s)
new=time.asctime(s)
print(new)
1611303959.5288363
time.struct_time(tm_year=2021, tm_mon=1, tm_mday=22, tm_hour=8, tm_min=25, tm_sec=59, tm_wday=4, tm_yday=22, tm_isdst=0)
Fri Jan 22 08:25:59 2021
6.ctime函数可以把时间戳转化为可读形式
import time
now=time.time()
print(now)
s=time.ctime(now)
print(s)
1611304057.0304146
Fri Jan 22 16:27:37 2021
7.sleep函数推迟调用线程的运行,参数为秒
import time
print("start:",time.ctime())
time.sleep(9)
print("end:",time.ctime())
start: Fri Jan 22 16:29:58 2021
end: Fri Jan 22 16:30:07 2021
8.strftime函数接收时间元组,返回可读的当地时间
9.strptime函数可以将时间字符解析为时间元组
六.datetime
1.date对象
import datetime
print(datetime.MAXYEAR) #支持的最大年份
print(datetime.MINYEAR) #支持的最小年份
print(datetime.date.today()) #today返回当天日期
print(datetime.date.today().weekday()) #weekday放回当天的星期
print(datetime.date.today().isoformat()) #返回IOS格式
9999
1
2021-01-22
4
2021-01-22
2.time对象
import datetime
print(datetime.time()) #默认时间
print(datetime.time.max) #支持的最大时间
print(datetime.time.min) #支持的最小时间
00:00:00
23:59:59.999999
00:00:00
3.datetime对象
datetime=date+time,同时返回日期和时间
import datetime
today=datetime.datetime.today()
print(today)
print(datetime.datetime.now()) #与today用法相同
print(datetime.datetime.utcnow()) #0时区时间
now=datetime.datetime.now()
print(now.date()) #date对象返回日期
print(now.time()) #time对象返回时间
2021-01-22 18:22:42.380595
2021-01-22 18:22:42.382589
2021-01-22 10:22:42.382589
2021-01-22
18:22:42.382589
4.timedelta对象
表示时间差
import datetime
dt1=datetime.datetime.now()
dt2=datetime.timedelta(weeks=2)+dt1
print(dt1)
print(dt2)
print(dt1-dt2)
2021-01-22 18:34:31.429461
2021-02-05 18:34:31.429461
-14 days, 0:00:00
5.tzinfo对象
七.calendar
1.calendar.isleap用于判断闰年
import calendar
print(calendar.isleap(2000))
print(calendar.isleap(2018))
True
False
2.canlender.leapdays用于返回两个年份之间闰年的总数
import calendar
print(calendar.leapdays(1900,2000))
24
3.canlender.month方法用于制作日历
四个参数:theyear,themonth,w(字符间距),l(行间距)
import calendar
print(calendar.month(2021,1))
4.calendar.monthcalender方法返回一个单层嵌套列表,每个列表里面装载一个星期
import calendar
print(calendar.monthcalendar(2021,1))
[[0, 0, 0, 0, 1, 2, 3], [4, 5, 6, 7, 8, 9, 10], [11, 12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30, 31]]
5.calendar.monthrange方法返回两个整数组成的元组,第一个数表示该月的第一天是星期几,第二个数表示改月的天数
import calendar
print(calendar.monthrange(2021,1))
(4, 31)
6.calendar.weekday方法返回给定日期的星期码
import calendar
print(calendar.weekday(2021,1,1))
4
7.calendar.calendar返回整年月历
import calendar
print(calendar.calendar(2021))