30
DecProgram to Check Leap Year in Python
Calculate the leap year in Python
The leap years are a special year in our calendar. It just adds an extra day to February every four years. But, not every year gets this special extra day! Now I am sure you have got one question about how to recognize whether the year is a leap year or not. That's why I brought you a Python program that will determine the leap year.
So, In this Python Tutorial, We will calculate a leap year using Python which will contain rules for determining if a year is a leap year, The leap-year program in Python in detail with code examples, how to check Leap Year Using Macros, So Let's see one by one.
How to check a leap year?
The logic :
Now Look at the following program to understand its implementation in Python Compiler.
Example
def CheckLeap(Year):
# Checking if the given year is leap year
if((Year % 400 == 0) or (Year % 100 != 0) and (Year % 4 == 0)):
print("Yes! This Year is a leap Year");
# Else it is not a leap year
else:
print ("This Year is not a leap Year")
# Taking an input year from user
Year = int(input("Enter the number here: "))
# Printing result
CheckLeap(Year)
Output
Enter the number here: 1700
Yes! This year is not a leap Year
Explanation
See, We have executed all three conditions that we have above in the program using the 'and' and 'or' keywords inside the if else condition. After getting the input from the user, the program first, and then we will go to the input part and check if the given year is a leap year. If the condition is satisfied, the program will print 'Leap Year'; else the program will print 'Not a leap year.The Leap Year Program in Python: For Loop
def is_leap_year(year):
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
return True
else:
return False
else:
return True
else:
return False
start_year = int(input("Enter the start year: "))
end_year = int(input("Enter the end year: "))
print("Leap years between", start_year, "and", end_year, "are:")
for year in range(start_year, end_year + 1):
if is_leap_year(year):
print(year)
Output
Enter the start year: 2020
Enter the end year: 2024
Leap years between 2020 and 2024 are:
2020
2024
Explanation
By applying the leap year logic and utilizing the Python code with the for loop provided, you'll be able to effortlessly determine whether a given year is a leap year or not.Program to Check Leap Year: Macros
Creating Constants:
Leap_Condition1=4
Leap_Condition2=100
Leap_Condition3=400
Defining the Function of a Leap Year:
def is_leap year(year):
if (year % Leap_Condition1 == 0 and year %
Leap_Condition2 ! = 0 ) or ( year % Leap_Condition3 == 0 ):
return True
else :
return False
Example
def is_leap_year(year):
# Function to check if a year is a leap year
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
return True
else:
return False
years_to_check = [2000, 2018, 2100, 2024]
for year in years_to_check:
if is_leap_year(year):
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")
Output
2000 is a leap year
2018 is not a leap year
2100 is not a leap year
2424 is a leap year
Python Short Solution of Leap Year Program
1. Leap Year Program in Python: Nested-if Condition
year = 2022
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")
else:
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")
Output
2022 is not a leap year
2. Leap Year Program in Python: if-else Condition
This method uses a single if-else statement to check leap year conditions directly.year = 2020
if ( year % 4 == 0 and year % 100 != 0 ) or ( year % 400 == 0 ):
print ( f "{year} is a leap year .")
else :
print(f "{year} is not a leap year.")
3. Leap Year Program in Python Using the Calendar Module
The calendar module's isleap() function simplifies leap year determination.import calendar
year = 2000
if calendar.isleap(year):
print(f"{year} is a leap year.")
else :
print(f" {year} is not a leap year.")
Leap Year Program in Python: While Loop
while True:
try:
year = int(input("Enter a year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")
break # Exit the loop if valid input is provided
except ValueError:
print("Wrong Year. Please enter a valid year.")
Output
Enter a year: 2024
2024 is a leap year.