• Python:实现日历功能


    背景        

            日常生活中,每天都要用到日历,日历成为我们生活中的必需品,那么如何制作日历呢,其实方法有很多,可以直接在excel中制作,也可以手画等等。

            学习过编程的朋友,能否想到用Python编写一个日历呢??Python可谓是功能强大,只有你想不到,没有python做不到,,,Python标准库中的calendar模块就可以做到这件事。

    实现

    输出某月日历

            下面是一个简单的示例,显示当前月份的日历:

    import calendar


     

    year = int(input("请输入年份: "))

    month = int(input("请输入月份: "))

                     

    #year = 2023

    #month = 10

                     

    #打印某个月的日历

    print(calendar.month(year,month))

    运行结果如下:

    输出某年日历

            上面的代码使用了calendar.month()函数,它会返回一个字符串,表示指定年份和月份的日历。我们也可以使用calendar.calendar()函数来打印一个完整的年历。

    以下是一个打印当前年份的完整年历的示例代码:

    import calendar


     

    year = int(input("请输入年份: "))

    month = int(input("请输入月份: "))

                     

    #year = 2023

    #month = 10

                     

    #打印某个月的日历

    #print(calendar.month(year,month))

    # 打印年历

    print(calendar.calendar(year))

    运行结果如下:

    设置周日为第一天        

    可以看到上面日历是以星期一为第一天,calendar还提供了设置一周内的某一天为第一天的函数,请看以下示例(以周日为第一天):

    import calendar


     

    year = int(input("请输入年份: "))

    month = int(input("请输入月份: "))

                     

    #year = 2023

    #month = 10

                     

    #打印某个月的日历

    #print(calendar.month(year,month))

    #设置周日为第一天

    calendar.setfirstweekday(firstweekday=6)

    # 打印年历

    print(calendar.calendar(year))

    运行结果如下:

    高级 

            当然python还有更好玩儿的实现日历的方法,例如想要生成日历到excel中,如

    !!!请参照以下实现!!!

    Python:实现日历到excel文档

  • 相关阅读:
    Leetcode力扣题解 - 30.串联所有单词的子串
    TS —— TS的基础知识点
    辉芒微IO单片机FT60F025-TRB
    Dynamo批量操作之提取族库族参数写入Excel
    python 如何判断一组数呈上升还是下降趋势
    第二章:C语言基础之数据类型+常量+变量+转义字符
    二十一、C位域
    漏洞深度分析|Apache Airflow example_bash_operator DAG 远程代码执行漏洞
    sqlserver连接
    FEBAN 增强
  • 原文地址:https://blog.csdn.net/hhd1988/article/details/134043767