Python Glossary Русский

all Python 3calendarfunction

calendar.monthcalendar()

dates and leap years

Returns a month as a list of weeks, each a list of 7 numbers; days of the neighbouring months are replaced by zeros.

Syntax

calendar.monthcalendar(year, month)

Zeros appear at both the start and the end: skip them when iterating or you get a 'day 0'. The order of days within a week depends on the global setfirstweekday(), so index 0 is not always Monday. A month has 4, 5 or 6 weeks — do not assume a fixed number.

Examples

import calendar
weeks = calendar.monthcalendar(2024, 2)
print(weeks[0], len(weeks))  # → [0, 0, 0, 1, 2, 3, 4] 5
fridays = [w[calendar.FRIDAY] for w in weeks if w[calendar.FRIDAY]]
print(fridays)  # → [2, 9, 16, 23]

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy