How to Make a Calendar in Python


In this example we will show the methods to generate a calendar in python.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

import  calendar as cal
# import

# Define the function, the calendar module is explicitly called inside the function, returning the month of the specified year
def get_month(year, month):
    return cal.month(year, month)

# Enter the specified year and month
yy = int(input("please input year: "))
mm = int(input("please input month: "))

# Display the calendar, if there is only the year parameter, return the calendar for all the months of the year.
print(get_month(yy,mm))

Output:

please input year: 2019
please input month: 10
    October 2019
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


please input year: 2018
please input month: 8
    August 2018
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
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments