
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Calendar in python
The calendar module in python has features to handle all the features related to calendar and dates. It is one of the very extensively used module which has many in-built functions to use dates in the python programs.
Example
import calendar print(calendar.calendar(2019))
Output
Running the above code gives us the following result −
2019 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 3 4 5 6 1 2 3 1 2 3 7 8 9 10 11 12 13 4 5 6 7 8 9 10 4 5 6 7 8 9 10 14 15 16 17 18 19 20 11 12 13 14 15 16 17 11 12 13 14 15 16 17 21 22 23 24 25 26 27 18 19 20 21 22 23 24 18 19 20 21 22 23 24 28 29 30 31 25 26 27 28 25 26 27 28 29 30 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 4 5 6 7 1 2 3 4 5 1 2 8 9 10 11 12 13 14 6 7 8 9 10 11 12 3 4 5 6 7 8 9 15 16 17 18 19 20 21 13 14 15 16 17 18 19 10 11 12 13 14 15 16 22 23 24 25 26 27 28 20 21 22 23 24 25 26 17 18 19 20 21 22 23 29 30 27 28 29 30 31 24 25 26 27 28 29 30 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 4 5 6 7 1 2 3 4 1 8 9 10 11 12 13 14 5 6 7 8 9 10 11 2 3 4 5 6 7 8 15 16 17 18 19 20 21 12 13 14 15 16 17 18 9 10 11 12 13 14 15 22 23 24 25 26 27 28 19 20 21 22 23 24 25 16 17 18 19 20 21 22 29 30 31 26 27 28 29 30 31 23 24 25 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 3 4 5 6 1 2 3 1 7 8 9 10 11 12 13 4 5 6 7 8 9 10 2 3 4 5 6 7 8 14 15 16 17 18 19 20 11 12 13 14 15 16 17 9 10 11 12 13 14 15 21 22 23 24 25 26 27 18 19 20 21 22 23 24 16 17 18 19 20 21 22 28 29 30 31 25 26 27 28 29 30 23 24 25 26 27 28 29 30 31
Specific Month
We can get a specific month from the calendar by using the month function with proper parameters.
Example
import calendar print (calendar.month(2019,7))
Output
Running the above code gives us the following result −
July 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
Number of Days in a Month
We can find the number of days in a month by using the function monthrange(). It also gives the weekday of the first day of the month. In the below example we find out the number of days in Feb 2019 and also get the weekday number of the first day of February.
Example
import calendar print(calendar.monthrange(2019,2))
Output
Running the above code gives us the following result −
(4, 28)
Leap Year
The function isleap() is used to find if a year is leap year or not. We can also use the function leapdays() to calculate the number of leap years between a range of two given years. In the below example we find if the year 2019 and 2020 are leap years or not. And also find the number of leap years between the years 2000 and 2020.
Example
import calendar print(calendar.isleap(2019)) print(calendar.isleap(2020)) print(calendar.leapdays(2000, 2020))
Running the above code gives us the following result:
Output
False True 5
- Related Articles
- Calendar Functions in Python - ( calendar(), month(), isleap()?)
- Calendar function in Python
- Calendar in Python Program
- Calendar Functions in Python | Set 1( calendar(), month(), isleap()…)
- Print a Calendar in Python
- The calendar Module in Python
- Explain calendar module in python?
- What is calendar module in python?
- Getting calendar for a month in Python
- Calendar Functions in Python -(monthrange(), prcal(), weekday()?)
- How to Display Calendar using Python?
- How to print calendar for a month in Python?
- How to print a calendar for a month in Python
- Gregorian Calendar in Java
- Calendar Functions in Java
