Software Testing | How to use Python to display a calendar for a specified year

Insert image description here

Preface

How to print calendar in Python? Python provides a built-in module Calendarto print and display the calendar, and also provides many calendar-based operations. This article will introduce how to use Python to print and display the calendar, and perform corresponding operations on the calendar.

How to use calendar in Python

CalendarIt is a built-in module of Python, we can just quote it directly. The calendar module has many classes and functions that can be used to print an idealized calendar. The calendar starts on Monday and ends on Sunday. In the code below, we print a specific month using the calendar.month() function, which takes two parameters year and month. Examples are as follows:

import calendar

year = 2023
month = 9

print(calendar.month(year, month))

------------------
输出结果如下:
   September 2023
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

Of course, we can also print a calendar for a whole year. For example, we want to print a calendar for 2023, as follows:

import calendar

print(calendar.calendar(2023))

--------------
输出结果如下:
                                  2023

      January                   February                   March
Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
                   1             1  2  3  4  5             1  2  3  4  5
 2  3  4  5  6  7  8       6  7  8  9 10 11 12       6  7  8  9 10 11 12
 9 10 11 12 13 14 15      13 14 15 16 17 18 19      13 14 15 16 17 18 19
16 17 18 19 20 21 22      20 21 22 23 24 25 26      20 21 22 23 24 25 26
23 24 25 26 27 28 29      27 28                     27 28 29 30 31
30 31

       April                      May                       June
Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
                1  2       1  2  3  4  5  6  7                1  2  3  4
 3  4  5  6  7  8  9       8  9 10 11 12 13 14       5  6  7  8  9 10 11
10 11 12 13 14 15 16      15 16 17 18 19 20 21      12 13 14 15 16 17 18
17 18 19 20 21 22 23      22 23 24 25 26 27 28      19 20 21 22 23 24 25
24 25 26 27 28 29 30      29 30 31                  26 27 28 29 30

        July                     August                  September
Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
                1  2          1  2  3  4  5  6                   1  2  3
 3  4  5  6  7  8  9       7  8  9 10 11 12 13       4  5  6  7  8  9 10
10 11 12 13 14 15 16      14 15 16 17 18 19 20      11 12 13 14 15 16 17
17 18 19 20 21 22 23      21 22 23 24 25 26 27      18 19 20 21 22 23 24
24 25 26 27 28 29 30      28 29 30 31               25 26 27 28 29 30
31

      October                   November                  December
Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
                   1             1  2  3  4  5                   1  2  3
 2  3  4  5  6  7  8       6  7  8  9 10 11 12       4  5  6  7  8  9 10
 9 10 11 12 13 14 15      13 14 15 16 17 18 19      11 12 13 14 15 16 17
16 17 18 19 20 21 22      20 21 22 23 24 25 26      18 19 20 21 22 23 24
23 24 25 26 27 28 29      27 28 29 30               25 26 27 28 29 30 31
30 31

Other uses of calendar module

  1. Determine leap year

We can use it calendar.isleap()to determine whether the year is a leap year. The return value is a Boolean value. True means a leap year, False means a normal year. The code is as follows:

import calendar

print(calendar.isleap(2023))
print(calendar.isleap(1900))
print(calendar.isleap(2024))

--------------
输出结果如下:
False
False
True
  1. Returns the total number of leap years between two years

We can use calendar.leapdays()the following code to return the total number of leap years between two years:

import calendar

print(calendar.leapdays(1995, 2023))
print(calendar.leapdays(1900, 2023))
  1. Returns the starting week of the month and the number of days in the month

Use to calendar.monthrange()return a tuple data (two integers), the first one: represents the starting week number of this month (0: Monday... 6: Sunday), the second one: represents the date number of the last day of this month, that is, the month days

import calendar

print(calendar.monthrange(2023, 8))

--------
输出结果如下:
(1, 31)

Summarize

This article mainly introduces the use of calendarmodules to print out calendars, and introduces calendarother uses of modules, which we can use based on the examples in this article.

Guess you like

Origin blog.csdn.net/Tester_muller/article/details/132583957