Detecting Leap Year in Python
When determining whether a year is a leap year using mathematical methods, several operations must be performed sequentially, which is not very convenient. However, Python has the ability to significantly simplify this process using a special method isleap
of the calendar
module. The desired year is passed to the method parameter. The method returns Boolean values. If the year is a leap year, True
is output, otherwise - False
.
First, you need to import the calendar
module:
import calendar
Next, we apply the isleap
method to the imported module. We write the result to the res
variable and display it on the screen:
res = calendar.isleap(2025)
print(res) # False
Given year:
year = 2020
Determine if it is a leap year.
Given year:
year = 1910
Determine if it is a leap year.
Determine whether the current year is a leap year.