Python中的日历函数 - (calendar(),month(),isleap()?)
我们可以使用Python中的**calendar**模块执行日历操作。在这里,我们将学习**calendar**类实例的不同方法。
calendar.calendar(year)
**calendar**类实例返回该年的日历。让我们看一个例子。
示例
# importing the calendar module import calendar # initializing year year = 2019 # printing the calendar print(calendar.calendar(year))
输出
如果您运行以上代码,您将获得以下结果。

calendar.firstweekday()
方法**calendar.firstweekday()**返回一周中的第一天,即星期一。
示例
# importing the calendar import calendar # getting firstweekday of the year print(calendar.firstweekday())
输出
如果您运行以上程序,您将获得以下结果。
0
calendar.isleap(year)
方法**calendar.isleap(year)**返回该年是否为闰年。让我们举一个例子。
示例
# 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')输出
如果您运行以上代码,您将获得以下结果。
2019 is not a leap year 2020 is leap year
calendar.WEEK_CONSTANT
在**calendar**模块中有一些**WEEK_CONSTANTS**用于获取星期几的数字。让我们看一些例子。
示例
# 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}')输出
如果您运行以上程序,您将获得以下结果。
Monday weekday: 0 Tuesday weekday: 1 Wednesday weekday: 2 Thursday weekday: 3 Friday weekday: 4 Saturday weekday: 5 Sunday weekday: 6
结论
在本教程中,我们学习了一些有用的方法。如果您对本教程有任何疑问,请在评论区中提出。
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C编程
C++
C#
MongoDB
MySQL
Javascript
PHP