Python calendar module | leapdays() method
Calendar module allows to output calendars like program, and provides additional useful functions related to the calendar. Functions and classes defined in Calendar module use an idealized calendar, the current Gregorian calendar extended indefinitely in both directions.
In Python, calendar.leapdays() is a function provided in calendar module for simple text calendars.
leapdays() method is used to get number of leap years in a specified range of years.
Syntax: leapdays() Parameter: year1, year2: years to get the number of leap years. Returns: Returns number of leap years in a specified range.
Code #1:
# Python program to explain working of leapdays() method # importing calendar module import calendar # checking number of leap years in range print(calendar.leapdays(2016, 2022)) print(calendar.leapdays(2001, 2003)) |
Output:
2 0
Code #2: Explaining working of leapdays() method.
Below code prints number of leap years and 1st-month calendar of last leap year if any leap year found in given range, otherwise notifies no year is leap.
# Python code to demonstrate the working of leapdays() # importing calendar module for calendar operations import calendar year1 = 2005year2 = 2025 # calling leapdays() method to verify val = str(calendar.leapdays(year1, year2)) print("Number of leap years found is % s" % val) count = 0# checking the condition is True or not for year in range(year1, year2): val = calendar.isleap(year) if val == True: lyear = year count += 1 if count >= 1: # print 1th month of first leap year calendar.prmonth(lyear, 1, 2, 1) # Returned False, year is not a leap else: print("No leap year found") |
Output:
Number of leap years found is 5
January 2024
Mo Tu We Th Fr Sa Su
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
Recommended Posts:
- Python calendar module | prmonth() method
- Python calendar module | iterweekdays() method
- Python calendar module | isleap() method
- Python calendar module | monthdays2calendar() method
- Python calendar module | monthdayscalendar() method
- Python calendar module | pryear() method
- Python calendar module | yeardayscalendar() method
- Python calendar module | monthdatescalendar() method
- Python calendar module | itermonthdays() method
- Python calendar module | itermonthdays2() method
- Python calendar module | formatmonth() method
- Python calendar module | yeardatescalendar() method
- Python calendar module | setfirstweekday() method
- Python calendar module | yeardays2calendar() method
- Python calendar module | formatyear() method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



