For a better understanding of this example, we always recommend you to learn the basic topics of Python programming listed below:
Leap year is a year with 366 days in it. Practically, we understand that is to check the month February has 29 days in it. Now we have to know how we check that in a python program.
Leap year is a year that is divisible by 4. This solution will work for all years except for century-year [ years which are ending in 00]. We have to check that it is divisible by 400; if that is perfectly divisible by 400, it will be a leap year.
In the python program, so we have to use the if
condition and elif
statement to check if that is divisible by 4 or 400 to confirm the year is a leap year or not. Python language uses the Mod operator for doing the divisible checking.
STEP 1: Accept a value for the year from the user. (python programming language accepts only string using the input function, so we have to convert that string to integer before saving the value using the int operator).
STEP 2: Use the if condition and mod operator to check the given year is divisible by 4, and it's not divisible by 100. then print the given year as a leap year.
STEP 3: Use the elif
statement to check if the year is perfectly divisible by 100, then print the given year is not a leap year.
STEP 4: Use the elif
statement to check the given year is divisible by 400, print the year is a leap year.
STEP 5: Use the else
statement in python language to print the given year is not a leap year.
year = int(input("Please enter the year to check: ")) # accept the year from user
# Leap Year Check
if (
year % 4 == 0 and year % 100 != 0
): # checking the year is divisible by 4 and not divisible by 100
print(year, "it is a leap year") # print its a leap year
elif year % 100 == 0:
# check the year divisible by 100
print(year, "Entered value is not a leap year")
elif year % 400 == 0:
# checking the year is divisible by 400
print(year, "it is a leap year")
else:
print(year, "Entered value is not a leap year")
Please enter the year to check: 2004 2004 it is a leap year