Python 生成日历
以下代码用于生成指定日期的日历:
实例(Python 3.0+)
# Filename : test.py
# author by : www.runoob.com
# 引入日历模块
import calendar
# 输入指定年月
yy = int(input("输入年份: "))
mm = int(input("输入月份: "))
# 显示日历
print(calendar.month(yy,mm))
执行以上代码输出结果为:
输入年份: 2015
输入月份: 6
June 2015
Mo Tu We Th Fr Sa Su
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
Python3 实例
dll
cqp***l@163.com
这个代码的缺点就是,我们日常用的日历都是星期天在前的。所以改进代码,应该加一行用以将星期天放在首位。
#生成日历 # 引入日历模块 import calendar # 输入指定年月 yy = int(input("输入年份: ")) mm = int(input("输入月份: ")) calendar.setfirstweekday(firstweekday=6)#设置第一天是星期天 # 显示日历 print(calendar.month(yy,mm))dll
cqp***l@163.com
Rounie
rou***jane@163.com
参考地址
import calendar calendar.setfirstweekday(firstweekday=6) # 显示出一年 12 个月份的日历 while True: yy = int(input('input years:')) # mm = int(input('input month:')) for i in range(12): print(calendar.month(yy, i + 1)) print('*' * 20)借鉴楼上的设置日期显示格式,将第一天设置为周日,写的一个显示一年 12 月份的日历。
Rounie
rou***jane@163.com
参考地址
python小鬼
313***4994@qq.com
输出一年12个月:
import calendar year = int(input('输入年份:')) for a in range(1,13): print('---------------------') print(calendar.month(year,a))python小鬼
313***4994@qq.com