Python tutorial
Calendar module, has various properties for days,weeks and months. and set of methods to display year month calendar in various formats like text, HTML,. Based on specific culture, calendar headers will display specific to that region or country. User can set weekday as MonDay or any other day of his/her choice.
day_name and day_abbr properties displays full-form of day the and short form of the day respecitively
import calendar as c for day_full in c.day_name:print(day_full,end = ' ') for day_short in c.day_abbr:print(day_short,end = ' ') Monday Tuesday Wednesday Thursday Friday Saturday Sunday >>> Mon Tue Wed Thu Fri Sat Sun >>>
Calendar module has properties for WeekDays, each WeekDay has value
import calendar as c >>>c.MONDAY 0 >>>c.FRIDAY 4 >>>c.SUNDAY 6Similarly Month Full Names and Month Abbrevations can get using month_name and month_abbr respecitively.
for month in c.month_name: print(month, end=' ') for month in c.month_abbr: print(month, end=' ') January February March April May June July August September October November December >>> Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec >>>Check Weekday using weekday function Weekday method accepts year,month and day parameters, returns integer from 0-6. 0-MonDay 1-TuesDay ... 6-Sunday So following example year 2000 Jan 1st comes on SATURDAY
>>> c.weekday(2000,1,1) 5 >>> c.weekday(2000,1,1)==c.SATURDAY True >>>Week headers in Calendar Month By default firstweekday is monday, so week header has Mon thru Sunday
>>> c.weekheader(4) 'Mon Tue Wed Thu Fri Sat Sun ' >>> c.weekheader(5) ' Mon Tue Wed Thu Fri Sat Sun ' >>> c.weekheader(10) ' Monday Tuesday Wednesday Thursday Friday Saturday Sunday ' >>> c.setfirstweekday(6) >>> c.weekheader(10) ' Sunday Monday Tuesday Wednesday Thursday Friday Saturday ' #Playing with width Width is 1 single letter weekdays, 2 two letter weekdays, 3 three letter weekdays etc., >>> c.weekheader(1) 'S M T W T F S' >>> c.weekheader(2) 'Su Mo Tu We Th Fr Sa' >>> >>>Month Range For a given year, find number of days in a month. monthrange method returns a tuple (start of the month, end of the month) for Year 2000 Jan month has 31 days. for Year 2000 Feb month has 29 days(because it is leap year).
>>> c.monthrange(2000,1) (5, 31) >>> c.monthrange(2000,2) (1, 29) >>> >>>#Find last day of the month >>>firstday,lastday = c.monthrange( 2000, 2) >>>print(lastday) 29month calendar for a given year, month calendar for a year can be calculated using c.monthcalendar method. returns list of weeks,each element in the list represents a week, each element in that sublist is day number, as shown below. 0 indicates in first sublist previous month, in last sublist indicates next month, so Year 2000 month February starts on WEDNESDAY. This month has 5 weeks and ends on 5th week WEDNESDAY.
>>> c.monthcalendar(2000,2) [[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, 0, 0, 0, 0]]Display Calendar Month Custom implementation of month calendar for a given Year and month. import calendar module, print weekheader using weekheader method using monthcalendar for year 2000 and month 2 returns list of weeks.
import calendar as c print(c.weekheader(5)) for days in c.monthcalendar(2000,2): print() for day in days: print('{0:<6}'.format(day),end=' ') print() #OUTPUT Mon Tue Wed Thu Fri Sat Sun 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 0 0 0 0 0Japanese Month Calendar to display any other calendar(other that your system's default configuartion), you need to change locale settings using locale module has setlocale method.Here setting it to 'Japanese' Calendar. and finally calling calendar module's different_locale method. Then call prmonth print method for a year and month.
>>> locale.setlocale(locale.LC_ALL,'Japanese') 'Japanese' >>> c.different_locale(locale.getlocale)Note: All Weekdays and month printed in Japanese language.(i.e assuming your system has installed Asia regional language package). Otherwise unknown characters will be displayed or setlocale method will throw an exception.>>> c.prmonth(2023,2) 2月 2023 月 火 水 木 金 土 日 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
leap year which has 366 days, non-leap years has 365 days. Leap year should be divisible by 4 and 100 or 400
c.leapyear(2000) True c.leapdays(2000,2023) 6Find given year is leap year or not
A leap year is if it is divisible by 4 but not by 100,except that year is divisible by 400 are leap years.
def isleap(year): if ((year%==0 and year%100) or year%400==0): return True return False isleap(2000) # True isleap(2020) # True isleap(2030) #FalseFind Leap Days between years
use Range object to iterate from year to toyear,inside for-each loop call above function isleap(year) , it it is leap year increment leapdays by 1 and print that year. finally print leap days between years
leapdays = 0; for year in range(2000,2023): if isleap(year): leapdays=leapdays+1 print(year) print("leap days between Year{} to Year{} are {}".format(2000,2023,leapdays)) #OUTPUT 2000 2004 2008 2012 2016 2020 leap days between Year2000 to Year2023 are 6
ADS