Introduction
Python is one of the most popular programming languages out there as of today. Python is easier to learn than most of the programming languages and it can also do a lot of cool stuff by using a very short amount of codes.
I thought about starting a kind of series type article writing here on Python where I will be writing about a lot of small but cool stuff that you can do easily using Python.
Don't worry if you are new to Python as I am also going to explain everything so that you would not face any problems understanding what I am doing and how those work.
Module (Calendar)
In this article, we will learn about a cool module of Python, and that is the Calendar module.
Preparation
Make sure that you have installed Python on your computer correctly. If you are on Windows operating system, then make sure to check the stuff by reading this article I wrote on freeCodeCamp.
Code with Explanations
I will code in a local machine. You can think of the local machine as your personal computer, in short. The extension of the python source file is .py
. Let's say, I am creating a python file named Main.py
.
Don't worry about the file icon as that might appear differently in your case, based on your default text editor or IDEs. As I am currently using the PyCharm as my default IDE for Python, the file icon is showing the icon of PyCharm itself.
Code
# import the calendar module first
import calendar
# Use a variable to store the year
year = 2022
#print the whole calendar of the given year
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
Explanations
Let's start reviewing our code and understand all of the lines sequentially.
As we want to work with the in-built Calendar
module, we need to import the module first. So, we imported the module first.
Then we used an integer variable to store the value of the year. Here, the year
is that integer variable. You can use any other variable name you want if that is not within the special keywords.
At last, we simply printed the whole year
calendar data.
Keep in mind that, we can also print the year directly within the print statement without using the year
variable at all. Like print(calendar.calendar(2022))
. It would directly print the entire calendar of the year 2022
in the console window.
You can also modify the result by adding/removing more parameters in the print statement if you want. However, I am leaving that task to you. If you want, then I can also write about some more amazing stuff you can do just using the Calendar
module in Python.
Conclusion
I hope this article has helped you to learn about the Calendar
module for the first time. Thank you so much for reading the entire article till now.
If you want to follow me or want to discuss something with me, then here you go!