在Python中,你可以使用 `calendar` 模块来判断某个月的天数。以下是使用 `calendar.monthrange` 函数的方法:
import calendardef get_days_in_month(year, month):_, num_days = calendar.monthrange(year, month)return num_days示例使用year = 2024month = 2 2月print(f"The number of days in {year} - {month} is {get_days_in_month(year, month)}")
如果你需要判断是否为闰年,可以使用 `calendar.isleap` 函数:
import calendardef is_leap_year(year):return calendar.isleap(year)示例使用year = 2024print(f"Is {year} a leap year? {is_leap_year(year)}")
如果你需要处理当前日期并查询当前月份的天数,可以使用 `datetime` 模块:
import datetimedef days_of_current_month():today = datetime.date.today()_, last_day = calendar.monthrange(today.year, today.month)return last_day示例使用print(f"The number of days in the current month is {days_of_current_month()}")
以上代码片段展示了如何使用Python标准库中的 `calendar` 和 `datetime` 模块来获取月份的天数。请根据你的具体需求选择合适的方法

