What is calendar module in python?



The Calendar module in Python is used to display calendars and provides useful Built-in functions for displaying week, week day, month, month of the year, and other operations. By default, these calendars have Monday as the first day of the week, and Sunday as the last.

Display the Calendar of an Year

To display the calendar of an year, use the calendar() method and set year as the parameter ?

Example

import calendar # Set the year year = 2022 # Display the calendar print(calendar.calendar(year))

Output

 2022 January February March Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su 1 2 1 2 3 4 5 6 1 2 3 4 5 6 3 4 5 6 7 8 9 7 8 9 10 11 12 13 7 8 9 10 11 12 13 10 11 12 13 14 15 16 14 15 16 17 18 19 20 14 15 16 17 18 19 20 17 18 19 20 21 22 23 21 22 23 24 25 26 27 21 22 23 24 25 26 27 24 25 26 27 28 29 30 28 28 29 30 31 31 April May June Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su 1 2 3 1 1 2 3 4 5 4 5 6 7 8 9 10 2 3 4 5 6 7 8 6 7 8 9 10 11 12 11 12 13 14 15 16 17 9 10 11 12 13 14 15 13 14 15 16 17 18 19 18 19 20 21 22 23 24 16 17 18 19 20 21 22 20 21 22 23 24 25 26 25 26 27 28 29 30 23 24 25 26 27 28 29 27 28 29 30 30 31 July August September Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su 1 2 3 1 2 3 4 5 6 7 1 2 3 4 4 5 6 7 8 9 10 8 9 10 11 12 13 14 5 6 7 8 9 10 11 11 12 13 14 15 16 17 15 16 17 18 19 20 21 12 13 14 15 16 17 18 18 19 20 21 22 23 24 22 23 24 25 26 27 28 19 20 21 22 23 24 25 25 26 27 28 29 30 31 29 30 31 26 27 28 29 30 October November December Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su 1 2 1 2 3 4 5 6 1 2 3 4 3 4 5 6 7 8 9 7 8 9 10 11 12 13 5 6 7 8 9 10 11 10 11 12 13 14 15 16 14 15 16 17 18 19 20 12 13 14 15 16 17 18 17 18 19 20 21 22 23 21 22 23 24 25 26 27 19 20 21 22 23 24 25 24 25 26 27 28 29 30 28 29 30 26 27 28 29 30 31 31 

Display the Month of a Calendar

To display the month of a calendar, use the calendar() method and set month as the parameter ?

Example

import calendar # Displays August month calendar print(calendar.month(2022,8))

Output

 August 2022 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 

Get the Weekday number

To get the weekday number, use the calendar.dayname i.e. for Monday, calendar.MONDAY, for Tuesday, calendar.TUESDAY, etc ?

Example

import calendar print(f'Monday Weekday Number: {calendar.MONDAY}') print(f'Tuesday Weekday Number: {calendar.TUESDAY}') print(f'Wednesday Weekday Number: {calendar.WEDNESDAY}') print(f'Thursday Weekday Number: {calendar.THURSDAY}') print(f'Friday Weekday Number: {calendar.FRIDAY}') print(f'Saturday Weekday Number: {calendar.SATURDAY}') print(f'Sunday Weekday Number: {calendar.SUNDAY}')

Output

Monday Weekday Number: 0 Tuesday Weekday Number: 1 Wednesday Weekday Number: 2 Thursday Weekday Number: 3 Friday Weekday Number: 4 Saturday Weekday Number: 5 Sunday Weekday Number: 6 

Check if an Year is a Leap Year

To check if an year is a leap year, use the calendar. isleap() method ?

Example

import calendar # Check if any of the Year is a Leap Year print(calendar.isleap(2020)) print(calendar.isleap(2021)) print(calendar.isleap(2022)) print(calendar.isleap(2023)) print(calendar.isleap(2024)) print(calendar.isleap(2025))

Output

True False False False True False 
Updated on: 2022-08-11T11:35:39+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements