用到了calendar模块
import calendar
# Returns weekday of first day of the month and number of days in month, for the specified year and month.
# 返回的第一个数加上1 对应的是 当月的第一天是星期几
calendar.monthrange(2020,1)
返回的结果是:
(2, 31)
表示的含义是2020年1月1日对应的是星期三,然后一月份有31天。
下面查看2020年每个月有多少天:
for i in range(1,13):
print(str(i)+"月有"+str(calendar.monthrange(2020,i)[1])+"天")
输出:
1月有31天
2月有29天
3月有31天
4月有30天
5月有31天
6月有30天
7月有31天
8月有31天
9月有30天
10月有31天
11月有30天
12月有31天