given a year, determine whether it is a leap year. if it is a leap year, return the boolean true, otherwise return false. note that the code stub provided reads from stdin and passes arguments to the is_leap function. it is only necessary to complete the is_leap function.
Given a year, determine whether it is a leap year.
Answer:
To determine whether a given year is a leap year in Python, we need to create a function called is_leap
. A leap year is a year that is divisible by 4 but not divisible by 100 unless it is divisible by 400. Here is a Python code snippet to accomplish this:
def is_leap(year):
if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
return True
else:
return False
year = int(input("Enter a year: "))
print(is_leap(year))
In this code:
- We define the
is_leap
function that takes ayear
as an argument. - We check if the year is divisible by 4 and not divisible by 100 unless it is also divisible by 400, then return
True
, otherwise returnFalse
. - We then read a year from the user using the
input
function and convert it to an integer. - Finally, we call the
is_leap
function with the input year and print the result, which will beTrue
if it’s a leap year andFalse
if it’s not.