Advertisement

掌握Python语言的年度日历制作方法

阅读量:

生成每月的日历代码

一、用python生成每月的日历代码

import calendar

def print_calendar(year):

创建一个 Calendar 对象

cal = calendar.Calendar()

生成特定年份的日历

遍历从1到12的所有月份:
输出该月的英文名称及指定年份:
print(calendar.month_name[month], year)
显示一周的缩略形式:
print("Mo Tu We Th Fr Sa Su")

遍历该月的所有日期块

for week in cal.monthdayscalendar(year, month):
# 遍历当前日历块中的每一天
for day in week:
if day == 0:
# 如果当前日期为零号
print(" ", end=" ")
else:
# 打印当前日期
print(f"{day:2d}"

全部评论 (0)

还没有任何评论哟~