A normal year has 365 days. Every four years, however, there is a leap year with 366 days. This is because the earth does not need exactly 365 days to travel once around the sun, but about a quarter day more. So a year has one day more every 4 years.
So that the calculation is correct, there is no leap year every 100 years. Nevertheless, every four hundred years a leap year takes place. This concerns the years 2000, 2400 etc.
def isLeapYear(year):
if (year % 4 != 0):
return False
elif (year % 400 == 0):
return True
elif (year % 100 == 0):
return False
else:
return True
year = 2020
if(isLeapYear(year)):
print(str(year) + " is a leap year")
else:
print(str(year) + " is not a leap year")
2020 is a leap year
isLeapYear(year)
This function takes a single argument year
and returns True
if the year is a leap year, and False
otherwise. Here’s how it works:
def isLeapYear(year):
if (year % 4 != 0):
return False
elif (year % 400 == 0):
return True
elif (year % 100 == 0):
return False
else:
return True
A year is considered a leap year if:
The function implements these rules as follows:
Check if the year is not divisible by 4:
if (year % 4 != 0):
return False
If the year is not divisible by 4, it is not a leap year.
Check if the year is divisible by 400:
elif (year % 400 == 0):
return True
If the year is divisible by 400, it is a leap year.
Check if the year is divisible by 100:
elif (year % 100 == 0):
return False
If the year is divisible by 100 (but not by 400), it is not a leap year.
Otherwise, it is a leap year:
else:
return True
After defining the isLeapYear
function, the code checks whether a specific year (2020 in this case) is a leap year and prints the result.
year = 2020
if(isLeapYear(year)):
print(str(year) + " is a leap year")
else:
print(str(year) + " is not a leap year")
year = 2020
Here, year
is set to 2020.
Call the isLeapYear
function and check its result:
if(isLeapYear(year)):
print(str(year) + " is a leap year")
else:
print(str(year) + " is not a leap year")
isLeapYear(year)
function is called with year
as its argument.True
, the code prints that the year is a leap year.False
, the code prints that the year is not a leap year.