How to display a calendar in Python

In this Python tutorial, we will discuss how to display a calendar in Python. Apart from this, we will check:

  • How to print whole year calendar in python
  • How to get the current year in Python
  • Calendar in python example
  • Python calendar.weekday() method
  • Python get the day of the week for a given date
  • Find the name of the month from a number in python
  • Python get a list of weeks in the month as date

Display calendar in Python

Now, let us see how to display a calendar in python.

  • We need to import the calendar module to display the calendar in python.
  • The built-in function month() inside the module takes in the year and month.
  • Then it will display the calendar for that month of the year.

Example:

import calendar
year = 2018
month = 12
print(calendar.month(year, month))

The output can be seen, once you will print “calendar.month(year, month)” then the output will appear. Here, the given year and month calendar will be displayed when we run the code.

You can refer to the below screenshot program to display a calendar in Python.

Program to display a calendar in Python
Program to display a calendar in Python

You may like Python Tkinter Stopwatch

Print whole year calendar in python

Let us see how to print the whole year calendar in python.

  • First, we need to import the calendar module
  • Then initialize the year
  • The built-in function calendar() inside the module takes the year
  • To print, the calendar for a particular year use the calendar.calendar(year)

Example:

import calendar
year = 2019
print(calendar.calendar(year))

The output can be seen, once you will print “calendar.calendar(year)” then the output will appear. Here, we imported the calendar module and after printing the whole year calendar of 2019 is displayed in the output.

You can refer to the below screenshot print whole year calendar in python.

Print whole year calendar in python
Print whole year calendar in python

Output:

This is the output of the above mentioned code and you can see the calendar of 2019 is displayed.

Print whole year calendar in python
Print whole year calendar in python

Python get the current year

Now, Let us see the below example on python get current year.

For getting the current year in python, we will first import datetime. Then we will use datetime.datetime.now().year to get the current year.

Example:

import datetime
current_y = datetime.datetime.now().year
print(current_y)

The output can be seen, once you will print “current_y” then the output will appear as “2020” the current year. Here, we imported the datetime module and after printing, it will return the current year in the output.

You can refer to the below screenshot for python get the current year.

Python get the current year
Python get the current year

Calendar in python example

  • Firstly, we will import calendar module
  • Here, s=calendar.TextCalendar(calendar.FRIDAY) creates a text calendar.
  • c_str = s.formatmonth(2022,1) are creating calendar for the year 2022, month 1-January.
  • c_str will print the output.

Example:

import calendar
s = calendar.TextCalendar(calendar.FRIDAY)
c_str = s.formatmonth(2022,1)
print(c_str)

The output can be seen, once you will print “c_str” then the output will appear. Here, we imported the calendar module and after printing, it will return the calendar of 2022 of the month January.

You can refer to the below screenshot for calendar in python example

Calendar in python example
Calendar in python example

Python calendar.weekday() method

The weekday() method is used to return the day of the week. So, 0 is Monday and 6 is Sunday which is the last day of the week.

Example:

import calendar
print(calendar.weekday(2020, 12, 20)

The output can be seen, once you will print “calendar.weekday()” then the output will appear as “6” which is Sunday. Here, we have imported the calendar module and after printing, it will return the value “6” which is Sunday.

You can check the below screenshot for python calendar.weekday() method.

Python calendar.weekday() method
Python calendar.weekday() method

Python get the day of the week for a given date

We will import the calendar module first and then weekday() function is used to get the day of the week for a given date.

Example:

import calendar
d = calendar.weekday(2020, 12, 22)
print(calendar.day_name[d])

The output can be seen, once you will print “calendar.day_name[d]” then the output will appear as “Tuesday”. Here, we have imported the calendar module and after printing, it will return the day of the week.

You can refer to the below screenshot for python get the day of the week for a given date.

Python get the day of the week for a given date
Python get the day of the week for a given date

Find the name of the month from a number in python

To get the month name from a number, we will use “calendar.month_name[]” it will return the full month name.

Example:

import calendar
print(calendar.month_name[12])

The output can be seen, once you will print “calendar.month_name[12]” then the output will appear as “December”. Here, we have imported the calendar module and after printing, it will return the name of the month from a number

You can refer to the below screenshot for find the name of the month from a number in python.

Find the name of the month from a number in python
Find the name of the month from a number in python

Python get a list of weeks in the month as date

The monthdayscalendar() function prints the list of weeks for a particular month and it take two arguments year and month and return the list of a week of the date.

Example:

import calendar
c = calendar.Calendar()
for week in c.monthdayscalendar(2020, 12):
    print(week)

The output can be seen, once you will print “week” then the output will appear. Here, we have imported the calendar module and for loop is used to iterate, and it will return the list of weeks for a particular month

You can see the below screenshot for python get a list of weeks in the month as date.

Python get a list of weeks in the month as date
Python get a list of weeks in the month as date

Python iterate through days of a month

To iterate through days of a month, we will use the function “itermonthdays()” and it will iterate the days of the given month and year.

Example:

import calendar
c = calendar.Calendar()
for day in c.itermonthdays(2021, 1):
    print(day)

The output can be seen, once you will print “day”. Here, we have imported the calendar module and for loop is used to iterate, and it will return the day of the mentioned month which is January.

In this below screenshot, we can see the code and output.

Python iterate through days of a month
Python iterate through days of a month

Output:

Python iterate through days of a month
Python iterate through days of a month

You may like the following Python tutorials:

In this Python tutorial, we learned:

  • How to display a calendar in Python
  • How to print the whole year calendar in python
  • How to get the current year in Python
  • Calendar in python example
  • Python calendar.weekday() method
  • Python get the day of the week for a given date
  • Find the name of the month from a number in python
  • Python get a list of weeks in the month as date
  • Python iterate through days of a month