Gregorian Month Boundaries contain two distinct facts: which weekday the first date occupies and how many dates belong to that month. Python's calendar.monthrange returns these facts together. Neither field is a complete printed calendar, and changing the displayed first weekday of a calendar grid does not change the actual weekday of the month's first date.
The first field is the weekday of day one, with Monday represented by zero and Sunday by six. The second is the number of days in the requested month. A weekday index of three therefore means Thursday; it does not mean that the month begins on its third date or has three preceding dates inside the month.
For the independent example February 2024, the numeric interpretation is first weekday three and day count twenty-nine. The first date is a Thursday. Both pieces are necessary for placing the dates in a grid, but the day count alone does not identify the weekday alignment.

A Gregorian leap year is divisible by four, except that years divisible by one hundred are excluded unless they are also divisible by four hundred. The original isleap function expresses these conditions directly using remainders. Merely checking divisibility by four misses the century exception.
The year 2000 is a leap year because it is divisible by four hundred. The year 1900 is not, even though it is divisible by four. February therefore has twenty-nine days in 2000 and twenty-eight in 1900. These fixed examples demonstrate the rule rather than relying on a rule of thumb about every fourth displayed calendar year.

The monthrange implementation validates the month, computes the weekday of its first date and looks up the ordinary day count. It adds one extra day only when the month is February and the year is a leap year. It does not add a day to every month of a leap year.
For example, the leap status does not turn April into a thirty-one-day month. A table of ordinary month lengths and the February adjustment have different roles. Keeping them separate makes it clear why one conditional extra day is enough, rather than rebuilding a whole year's month table for each query.
In a Monday-first grid, February 2024 needs three leading empty cells before its Thursday first date. If the displayed week instead begins on Sunday, the weekday index is still three under the API's Monday-zero convention, but the display offset becomes four cells.
For a first-column weekday f and first-date weekday w expressed in the same numbering, the offset is (w−f) modulo seven. The offset belongs to a layout decision. The original weekday belongs to the date. Confusing these two quantities can shift every date even though the monthrange call itself returned correct information.
A minimal seven-column display needs enough rows for the leading offset plus all dates. With Monday first, three leading cells and twenty-nine February dates occupy thirty-two cells, requiring five rows. The unused cells in that rectangle are layout space, not additional dates in February.
Some interfaces choose a fixed number of display rows or show adjacent-month dates in unused cells. Those choices do not change the returned day count. A user seeing five rows should not infer that the month contains thirty-five days, and a fixed six-row display should not be used as the source of month-length arithmetic.
The same two returned facts also determine the last date weekday. Starting from first weekday w and moving through d dates reaches weekday (w+d−1) modulo seven. February 2024 has twenty-eight transitions between its first and last dates, exactly four weeks, so both fall on Thursday. This arithmetic describes weekday placement only. It does not identify public holidays or count working days under a business calendar. Those concepts need additional location-specific or application-specific rules that are not returned by monthrange.
The module's setfirstweekday setting controls which weekday starts displayed weeks. Monthrange obtains the actual first-date weekday independently through the weekday calculation. The requested month must still be valid; a display preference does not make month zero or month thirteen into a valid Gregorian month.
A result from year and month also does not provide a time zone or a timestamp for an event. It describes calendar dates within the specified calendar convention. The site's calendar representation principles and date processing features connect date facts and layouts. Primary sources: Python's monthrange, weekday and display-setting contracts and the original Gregorian leap rule and month-boundary calculation. Both figures are original AI-assisted fixed-date examples.