Python calendar.monthcalendar() 方法
monthcalendar() 方法是 Python 中日曆模塊的內置方法。它適用於簡單的文本日曆並返回一個表示給定月份日曆的矩陣。矩陣中的每一行代表一整周,月份以外的天數用零表示。
模塊:
import calendar
用法:
monthcalendar(year, month)
參數:
year
:它是一個必需參數,代表日曆的年份。month
:它是一個必需參數,代表日曆的月份。
返回值:
這個方法的返回類型是<class 'list'>
(矩陣),表示給定年份的給定月份的日曆。
例:
# Python program to illustrate the
# use of monthcalendar() method
# importing calendar module
import calendar
year = 2020
month = 4
x = calendar.monthcalendar(year, month)
print("Calendar of April 2020 as a matrix")
for i in range(len(x)):
print(x[i])
print()
# Prints the calendar of the given month
# where 0 values are days out of that month
year = 1997
month = 1
a, b= calendar.monthrange(year, month)
x = calendar.monthcalendar(year, month)
print("Weekday starts from:", a)
print("Calendar of January 1997 as a matrix")
for i in range(len(x)):
print(x[i])
print()
# You can see the month starts from x[0][a]
year = 2000
month = 5
x = calendar.monthcalendar(year, month)
print("calendar of May 2020 as a matrix")
for i in range(len(x)):
print(x[i])
print()
calendar.setfirstweekday(6)
x = calendar.monthcalendar(year, month)
# Here column number 0 represents Sunday and so on
print("Calendar of May 2020 with first column representing Sunday")
for i in range(len(x)):
print(x[i])
輸出
Calendar of April 2020 as a matrix [0, 0, 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, 0, 0, 0] Weekday starts from:2 Calendar of January 1997 as a matrix [0, 0, 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, 31, 0, 0] calendar of May 2020 as a matrix [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, 31, 0, 0, 0, 0] Calendar of May 2020 with first column representing Sunday [0, 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, 31, 0, 0, 0]
相關用法
- Python calendar month()用法及代碼示例
- Python calendar monthrange()用法及代碼示例
- Python calendar firstweekday()用法及代碼示例
- Python calendar weekday()用法及代碼示例
- Python calendar isleap()用法及代碼示例
- Python calendar setfirstweekday()用法及代碼示例
- Python calendar leapdays()用法及代碼示例
- Python calendar weekheader()用法及代碼示例
- Python calendar calendar()用法及代碼示例
- Python calendar prmonth()用法及代碼示例
- Python calendar prcal()用法及代碼示例
- Python callable()用法及代碼示例
- Python string capwords()用法及代碼示例
- Python Functools cached_property()用法及代碼示例
- Python cmath.isclose()用法及代碼示例
- Python cmp()用法及代碼示例
- Python compile()用法及代碼示例
- Python cmath.log10()用法及代碼示例
- Python cmath.asinh()用法及代碼示例
- Python OpenCV cv2.rectangle()用法及代碼示例
注:本文由純淨天空篩選整理自 Python calendar Module | monthcalendar() Method with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。