Python Glossary Русский

all Python 3calendarfunction

calendar.isleap()

dates and leap years

Checks whether a year is a leap year in the Gregorian calendar: True or False.

Syntax

calendar.isleap(year)

The rule is not just divisible by 4: years divisible by 100 are not leap years unless they are divisible by 400 — 1900 is not, 2000 is. A hand-written year % 4 == 0 check fails exactly on such years. For the number of leap years in a range there is leapdays(), and February's length is easier to get from monthrange().

Examples

import calendar
print(calendar.isleap(1900), calendar.isleap(2000), calendar.isleap(2024))  # → False True True
print(1900 % 4 == 0)  # → True — наивная проверка ошибается

See also

Open in the full glossarydocs.python.orgReport an inaccuracy