常用模块
- calendar
- time
- datetime
- timeit
- os
- shutil
- zip
- math
- string
上述所有模块使用理论上都应该先导入,string是特例
calendar,time,datetime的区别参考中文意思
1. calendar模块
利用help命令查看calendar 包中的功能
FUNCTIONS
1-calendar =formatyear(theyear, w=2, l=1, c=6, m=3) method of TextCalendar instance
prcal 和calendar用法相似,直接打印某一年日历
获取一年的日历字符串,参数为:
- w = 每个日期之间的间隔字符数
- l = 每周所占用的行数
- c = 每个月之间的间隔字符数
- m=每行显示的月份数
import calendar
cal = calendar.calendar(2019,2,1,6,4)
print (cal)
print(type(cal))
输出:
2-isleap和leapdays
isleap判断一年是否为闰年
leapdays: 获取指定年份之间的闰年的个数
leapdays(y1, y2)
Return number of leap years in range [y1, y2).
Assume y1 <= y2.
import calendar
cal = calendar.isleap(2019)
print(cal)
cal2 = calendar.leapdays(2000,2019)
print(cal2)
输出:
False
5
3-month
prmonth用法同month, 直接打印某一个月的日历
获取某个月的日历字符串
格式:calendar.month(年,月)
回值:月日历的字符串
import calendar
cal = calendar.month(2019,9)
print (cal)
输出:
4-monthrange
获取某一年的某一个月从几号开始以及天数
monthrange(年份,月数)
返回值(周几开始,天数)0-6周一到周日
import calendar
cal = calendar.monthrange(2019,9)
w,d = calendar.monthrange(2019,9)
print (cal)
print(type(cal))
print("开始为:{0},天数为:{1}".format(w,d))
输出:
(6, 30)
<class 'tuple'>
开始为:6,天数为:30
5-monthcalendar
返回某一年某月的天数的二级列表
import calendar
cal = calendar.monthcalendar(2019,9)
print (cal)
print(type(cal))
输出:
[[0, 0, 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, 0, 0, 0, 0, 0, 0]]
<class 'list'>