Hello everyone , In the previous article, I introduced in detail time and datetime Use of modules . These two modules are more used to deal with time and date related problems .
This article introduces the third module calendar, Is mainly used to solve The calendar Problems in .
Chinese official website address :https://docs.python.org/zh-cn/3/library/calendar.html
This module defines many types , It mainly includes :Calendar、TextCalendar、HTMLCalendar, among Calendar yes TextCalendar and HTMLCalendar Base class of , These classes have very rich calendar processing methods .
meanwhile ISO 8601 The standard also stipulates 0 and Negative year .0 Year refers to... BC 1 year , -1 Year refers to... BC 2 year , And so on . Import before use :
import calendar
First we can look at 2021 The full calendar for the year :
By default :0 For Monday ,6 Sunday . We can go through setfirstweekday()
To set the first day of the week as Sunday (6) Or any other day
1、 By default : Monday is the first day
2、 Set any day
In the following example, we set Saturday as the first day
Check whether a year is a leap year . Yes, go back to True, Otherwise return to False
See how many leap years there are between two years . For example, in the second example , from 2000 To begin to 2021 In a total of 6 Leap years
Check a specific date is the day of the week . For example, in the second example ,11 month 1 It's Monday , use 0 Express
0: week 1
1: week 2
2: week 3
Return to the designated year 、 month What day is the first day And the number of days this month , Specific examples are as follows :
10 The first day of the month is Friday ,11 The first day of the month is Monday (0 On behalf of the Monday )
Through the perpetual calendar to verify :11.1 It happened to be Monday
Returns the calendar matrix of a month :
Each line represents 1 A few weeks
The default is from Monday , such as 11 In the 1 It happens to be Monday
Days outside this month are represented by zero
Returns the calendar of a month , The daily width interval is w character ,l Is the number of lines per week
The following is the default :
You can change the parameters w and l: This is equivalent to a control column width , A control row height
All use default parameters , View the full calendar of a year :
The default parameter form is used in the calendar results of the above output years . actually calendar Method has multiple parameters :
w: Width of each cell , The default is 2
l: Number of line breaks per column , The default is 1
c: The width of the interval between months , The default is 6
m: Divide the month into m Columns
The effect displayed after changing multiple parameters :
Welcome to reprint 、 Collection 、 Gain some praise and support !
At present, a technical exchange group has been opened , Group friends have exceeded 2000 people , The best way to add notes is : source + Interest direction , Easy to find like-minded friends