
- 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 Functions in Python - ( calendar(), month(), isleap()?)
We can perform the calendar operations using the calendar module in Python. Here, we are going to learn about the different methods of calendar class instance.
calendar.calendar(year)
The calendar class instance returns the calendar of the year. Let's see one example.
Example
# importing the calendar module import calendar # initializing year year = 2019 # printing the calendar print(calendar.calendar(year))
Output
If you run the above code, you will get the following results.
calendar.firstweekday()
The method calendar.firstweekday() returns the first weekday in the week i.e.., MONDAY.
Example
# importing the calendar import calendar # getting firstweekday of the year print(calendar.firstweekday())
Output
If you run the above program, you will get the following results.
0
calendar.isleap(year)
The method calendar.isleap(year) returns whether the year is a leap or not. Let's take one example.
Example
# importing the calendar module import calendar # initializing years year_one = 2019 year_two = 2020 # checking whether they are leap or not print(f'{year_one} is leap year' if calendar.isleap(year_one) else f'{year_one} is not a leap year') print(f'{year_two} is leap year' if calendar.isleap(year_two) else f'{year_two} is not a leap year')
Output
If you run the above code, you will get the following results.
2019 is not a leap year 2020 is leap year
calendar.WEEK_CONSTANT
There are WEEK_CONSTANTS in the calendar module to get the weekday number. Let's see examples.
Example
# importing the calendar module import calendar # getting weekday numbers print(f'Monday weekday: {calendar.MONDAY}') print(f'Tuesday weekday: {calendar.TUESDAY}') print(f'Wednesday weekday: {calendar.WEDNESDAY}') print(f'Thursday weekday: {calendar.THURSDAY}') print(f'Friday weekday: {calendar.FRIDAY}') print(f'Saturday weekday: {calendar.SATURDAY}') print(f'Sunday weekday: {calendar.SUNDAY}')
Output
If you run the above program, you will get the following results.
Monday weekday: 0 Tuesday weekday: 1 Wednesday weekday: 2 Thursday weekday: 3 Friday weekday: 4 Saturday weekday: 5 Sunday weekday: 6
Conclusion
We have learned some useful methods in this tutorial. If you have any queries regarding the tutorial, mention them in the comment section.
- Related Articles
- Calendar Functions in Python | Set 1( calendar(), month(), isleap()…)
- Getting calendar for a month in Python
- Calendar Functions in Java
- Calendar Functions in Python -(monthrange(), prcal(), weekday()?)
- How to print calendar for a month in Python?
- How to print a calendar for a month in Python
- Calendar in python
- Display Month of Year using Java Calendar
- Calendar function in Python
- Calendar in Python Program
- Increment a Month using the Calendar Class in Java
- Decrement a Month using the Calendar Class in Java
- Print a Calendar in Python
- The calendar Module in Python
- Explain calendar module in python?
