How to Print a Calendar in Python | Python provides a built-in function to print a calendar, also it allows to perform many operations based on the calendar, there are many ways to print the calendar. Let’s see how to print a python calendar. Also see:- Multiplication Table in Python
We will see these below Python program examples:-
- How to import calendar in python
- Python calendar monthrange
- Python calendar without module
- Print monthly calendar in python
- Print whole year calendar in python
How to Import Calendar in Python
Python provides a built-in module to “calendar” to print calendars and allows to perform various operations. The calendar module has many classes and functions which can be used to print idealized calendars. This calendar starts on Monday and ends on Sunday. In the below code, we print a particular month by using the calendar.month() function which two arguments year and month. We have printed the month of October.
import calendar
yy = 2022
mm = 10
print(calendar.month(yy, mm))
Output:
October 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
Python Calendar in Month Range
To print the calendar in python in the month range we have to use the calendar.monthrange() function. This calendar.monthrange() function is available in the calendar module which takes two integer arguments year and month and returns weekday of the first day of the month and number of days in the month. So in 2014 in the month of July, it had 31 days and the day was started on Monday.
import calendar
print(calendar.monthrange(2014,7))
Output:
(1, 31)
How to Print a Calendar in Python without Module
Printing a calendar without a calendar might be a quite tedious thing, as it requires coding some logic, but still, python allows us to achieve this by using an if-else loop with some mathematical operations which check for the days of the month and also check whether the year is a leap year or not. Now we print the year 2025 of the month September.
mm = 9
yy = 2025
month ={1:'January', 2:'February', 3:'March',
4:'April', 5:'May', 6:'June', 7:'July',
8:'August', 9:'September', 10:'October',
11:'November', 12:'December'}
day =(yy-1)% 400
day = (day//100)*5 + ((day % 100) - (day % 100)//4) + ((day % 100)//4)*2
day = day % 7
nly =[31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31]
ly =[31, 29, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31]
s = 0
if yy % 4 == 0:
for i in range(mm-1):
s+= ly[i]
else:
for i in range(mm-1):
s+= nly[i]
day += s % 7
day = day % 7
space =''
space = space.rjust(2, ' ')
print('Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa')
if mm == 9 or mm == 4 or mm == 6 or mm == 11:
for i in range(31 + day):
if i<= day:
print(space, end =' ')
else:
print("{:02d}".format(i-day), end =' ')
if (i + 1)% 7 == 0:
print()
elif mm == 2:
if yy % 4 == 0:
p = 30
else:
p = 29
for i in range(p + day):
if i<= day:
print(space, end =' ')
else:
print("{:02d}".format(i-day), end =' ')
if (i + 1)% 7 == 0:
print()
else:
for i in range(32 + day):
if i<= day:
print(space, end =' ')
else:
print("{:02d}".format(i-day), end =' ')
if (i + 1)% 7 == 0:
print()
Output:
Su Mo Tu We Th Fr Sa
01 02 03 04 05 06
07 08 09 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Print Monthly Calendar in Python
To print a monthly calendar we use a calendar.month() function which takes two arguments in the form of strings year and month and prints the specified year and month. Now we will print the year 2027 of the month of November. Python program to print the calendar of a given month and year.
import calendar
yy = 2027
mm = 11
print(calendar.month(yy, mm))
Output:
November 2027
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
How To Print Whole Year Calendar In Python
We use a calendar.Calendar() function to print the whole year in python which takes year as an argument, to use calendar.calendar() function we need to import calendar module available in python library
import calendar
print (calendar.calendar(2022))
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
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!